<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">板子:Atmega128L单片机和USB芯片ch374,</font></font></font><br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">功能:想实现电脑win10与单片机板子进行USB通信。</font></font></font><br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">参考的资料:是WCH南京沁恒微电子提供的相关资料。</font></font></font><br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">开发环境:ICCV7 for AVR(也就是AVR软件)</font></font></font><br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">测试现象:烧写程序后,win10计算机无法识别此USB设备(提示设备描述符请求失败)</font></font></font><br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">现在遇到的问题:SPI初始化后,数据很乱,和ch374提供的SPI时序图图示不一样。</font></font></font><br>
<br>
<font color="#333333"><font face="微软雅黑"><font style="font-size:18px">部分代码如下:</font></font></font><br><p><br></p><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">CH374_PORT_init</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span><span class="hljs-comment">//此函数在SPI_HW.C SPI_SW.C中都有出现。现在只使用此函数。</span>
</span>{
<span class="hljs-comment">/*如下采用《AVR单片机SPI通讯实例程序》中的spi_init()简单初始化函数的典型写法*/</span>
<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> temp;
IF(<span class="hljs-number">4</span> == SPI_LIN_NUM) <span class="hljs-comment">//spi为4线制 ★ ★ ★必须把MISO和MOSI引脚中间的电阻去掉 </span>
{
<span class="hljs-comment">/********** SPI复用功能引脚(PB0PB1PB2PB3)的初始化 及 配置 SPI的各寄存器配置 ****************/</span>
DDRB |= <span class="hljs-number">0x80</span>; <span class="hljs-comment">//MISO=input; MOSI,SCK,SS=output</span>
PORTB |= <span class="hljs-number">0x80</span>; <span class="hljs-comment">//MISO上拉电阻有效</span>
<span class="hljs-comment">/* SPCR=01011100 SPCR[7:4] 禁止SPI中断; SPI使能; 高位字节首先发送; 主机模式;*/</span>
<span class="hljs-comment">/* SPCR=01011100 SPCR[3:0] 时钟极性设置为11即SPI模式3(上升沿输入,下降沿输出);SPI时钟频率(sck)设置为00即1/4系统时钟速率*/</span>
<span class="hljs-comment">/* 禁止SPI片选 */</span>
<span class="hljs-comment">/** ★ ★ ★初始化后,SPI片选信号SCS#需要先拉高用来驱动从机的SS引脚(参考A128数据手册 P151 ss引脚功能)
参考ch374参考代码SPI_HW.C的CH374_SPI_SCS = 1; ★ ★ ★ */</span>
SCS1; <span class="hljs-comment">// 禁止SPI片选 </span>
<span class="hljs-comment">//SPCR = 0X5C; //SPI控制寄存器-SPCR //没有使用SPI中断方式进行SPI传输。</span>
SPCR = <span class="hljs-number">0X5D</span>;<span class="hljs-comment">//与陈品台沟通改了频率。</span>
SPSR = <span class="hljs-number">0x00</span>; <span class="hljs-comment">//SPI状态寄存器-SPSR</span>
temp = SPSR;
temp = SPDR; <span class="hljs-comment">//清空SPI,(和中断标志),使SPI空闲 //这一句话没弄懂。/*SPDR:数据寄存器(写寄存器发送,读寄存器接收)*/</span>
<span class="hljs-comment">/********** 下面是启用单片机的 ★外部中断INT0(PD0引脚) ★,ch374的INT#脚连接到单片机的PD0上 ****************/</span>
<span class="hljs-comment">/* 下面启用单片机的外部中断0,CH374的INT#引脚可以连接到单片机的中断引脚,中断为低电平触发;(猜测:用来侦测USB是否识别)
如果不使用中断,那么也可以用查询方式,由单片机程序查询CH374的INT#引脚是否为低电平 */</span>
DDRD &= ~(<span class="hljs-number">1</span> << PD0); <span class="hljs-comment">//配置PD0为输入</span>
PORTD |= (<span class="hljs-number">1</span><< PD0); <span class="hljs-comment">//配置上拉电阻有效</span>
EICRA = <span class="hljs-number">0X00</span>; <span class="hljs-comment">/* 设置中断敏感电平控制位 ISC01=0,ISC00 = 0;即设置外部信号为低电平时产生中断请求,触发中断处理函数 */</span>
EIFR = <span class="hljs-number">0</span>; <span class="hljs-comment">/* 清中断标志 */</span> <span class="hljs-comment">/*EIFR: 外部中断标志寄存器*/</span>
EIMSK = <span class="hljs-number">0X01</span>; <span class="hljs-comment">//外部中断0屏蔽寄存器等价于EIMSK |= 1<</span>
}
<span class="hljs-keyword">else</span> <span class="hljs-comment">//(3 == SPI_LIN_NUM) //spi为3根线MOSI和MISO共用一个引脚</span>
{ <span class="hljs-comment">//3线制的SPI</span>
DDRB |= <span class="hljs-number">0x80</span>; <span class="hljs-comment">//MISO=input; MOSI,SCK,SS=output</span>
PORTB |= <span class="hljs-number">0x80</span>; <span class="hljs-comment">//MISO上拉电阻有效</span>
SPCR = <span class="hljs-number">0xD5</span>; <span class="hljs-comment">//SPI允许,主机模式,MSB,允许SPI中断,极性方式01,1/16系统时钟速率</span>
<span class="hljs-comment">/*没有写完,**/</span>
}
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">USB_device</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{
<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> 0</span>
Wait374Interrupt();
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> 1</span>
<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)
{
<span class="hljs-keyword">if</span> ( Query374Interrupt( ))
USB_DeviceInterrupt( ); <span class="hljs-comment">// 等待USB设备中断,然后处理USB设备中断 </span>
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> 0</span>
FreeUSBmS = <span class="hljs-number">0</span>; <span class="hljs-comment">/* 清除USB空闲计时 */</span>
<span class="hljs-keyword">while</span>( <span class="hljs-number">1</span> )
{
<span class="hljs-keyword">if</span> ( FreeUSBmS >= <span class="hljs-number">250</span> )
{ <span class="hljs-comment">/* USB空闲超过250毫秒 */</span>
<span class="hljs-built_in">printf</span>( <span class="hljs-string">"Exit USB device mode "</span> );
CH374OffUSB( ); <span class="hljs-comment">/* 关闭USB设备 */</span>
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> ( FreeUSBmS < <span class="hljs-number">250</span> ) FreeUSBmS ++; <span class="hljs-comment">/* USB空闲计时,避免在USB通讯过程中由用户按键导致USB主从切换 */</span>
delay_1ms();
<span class="hljs-comment">/* USB设备模式全部在中断服务中处理,主程序可以做其它事情,当然也可以在主程序中使用查询方式处理USB设备的通讯 */</span>
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> </span>
}
<span class="hljs-function">BOOL <span class="hljs-title">Query374Interrupt</span><span class="hljs-params">( <span class="hljs-keyword">void</span> )</span>
</span>{
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> CH374_INT_line</span>
<span class="hljs-comment">// return((GetBit(PIND,PD0)) ? FALSE : TRUE );</span>
<span class="hljs-keyword">return</span>( CH374_INT_line ? FALSE : TRUE ); <span class="hljs-comment">/* 如果单片机PD0连接了CH374的中断引脚则直接查询此中断引脚的值:宏定义GetBit(Pin,Num) */</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span>
<span class="hljs-comment">//#define REG_INTER_FLAG 0x09 /*CH374芯片寄存器中的中断标志寄存器, 只读, 位写1清0 */</span>
<span class="hljs-comment">//#define BIT_IF_INTER_FLAG 0x0F /* 所有的USB中断标志 */</span>
<span class="hljs-keyword">return</span>( Read374Byte( REG_INTER_FLAG ) & BIT_IF_INTER_FLAG ? TRUE : FALSE ); <span class="hljs-comment">/* 如果未连接CH374的中断引脚则查询中断标志寄存器 */</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
}</code></pre>
<br>
<br>
<ignore_js_op>
<dl class="tattl attm">
<dd>
<img src="data/attach/1911/abuozxbc4sxk1y2r67pff2j5djkkl33q.png" alt="微信图片_20191105135937.png" title="微信图片_20191105135937.png">
</dd>
</dl>
</ignore_js_op>
<p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>