看了原子的串口通信、ADC,DAC,PWM,PWM+DAC视频,但是还是没有搞懂这个地方卡在哪了,还望有经验的师傅指点一二。谢谢了
以下是探索者STM32+串口转WiFi模块的无线通讯部分收到数据后输出DAC的代码
[mw_shl_code=applescript,true]while(1)
{
if(timex)timex--;
if(timex==1)LCD_Fill(30+54,100,239,112,WHITE);
t++;
delay_ms(10);
if(USART3_RX_STA&0X8000) //接收到一次数据
{
rlen=USART3_RX_STA&0X7FFF; //得到本次接收到的数据
USART3_RX_BUF[rlen]=0; //添加结束符
printf("%s",&USART3_RX_BUF[11]); //发送到串口
sprintf((char*)p,"收到%d字节,内容如下",rlen-11);//得到接收到实际有效数据的字节数
LCD_Fill(30+54,115,239,130,WHITE);
POINT_COLOR=BRED;
Show_Str(30+54,115,156,12,p,12,0); //显示接收到实际有效的数据长度
POINT_COLOR=BLUE;
LCD_Fill(30,130,239,319,WHITE);
strncpy(USART3_SEND_BUF,&USART3_RX_BUF[11],rlen-11);
Show_Str(30,130,180,190,&USART3_RX_BUF[11],12,0);//显示接收到实际有效数据
USART3_RX_STA=0;
if(constate!='+')t=1000; //状态还未连接,立即更新连接
else t=0; //状态为已经连接,10s后再检查
}
if(t==1000)//连续10s没有接收到任何数据,检查连接是否还存在
{
LCD_Fill(30+54,125,239,130,WHITE);
LCD_Fill(60,80,120,92,WHITE);
constate=atk_8266_consta_check();//得到连接
if(constate=='+')Show_Str(30+30,80,200,12,"连接成功",12,0); //连接状态
else Show_Str(30+30,80,200,12,"连接失败",12,0);
t=0;
}
if((t%20)==0)LED0=!LED0;
atk_8266_at_response(1);
Motor_control(&USART3_RX_BUF[11]);
}
[/mw_shl_code]
以下是while循环里的Motor_control(&USART3_RX_BUF[11])函数,传参暂时没用,先把DAC功能实现先
[mw_shl_code=c,true]void Motor_control(u8 * speed)
{
u16 adcx;
float temp;
u8 t=0,index =5;
u16 pwmval=0;
u8 key = 0 ;
POINT_COLOR=RED;
LCD_ShowString(30,50+50+70,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70+50+70,200,16,16,"PWM DAC TEST");
LCD_ShowString(30,90+50+70,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110+50+70,200,16,16,"2014/5/6");
LCD_ShowString(30,130+50+70,200,16,16,"WK_UP:+ KEY1:-");
POINT_COLOR=BLUE;//设置字体为蓝 {MOD}
LCD_ShowString(30,150+50+70,200,16,16,"DAC VAL:");
LCD_ShowString(30,170+50+70,200,16,16,"DAC VOL:0.000V");
LCD_ShowString(30,190+50+70,200,16,16,"ADC VOL:0.000V");
TIM_SetCompare2(TIM9,pwmval); //初始值
while(pwmval<250)
{
pwmval+=50;
TIM_SetCompare2(TIM9,pwmval); //输出
adcx=TIM_GetCapture2(TIM9);;
LCD_ShowxNum(94,150+120,adcx,3,16,0); //显示DAC寄存器值
temp=(float)adcx*(3.3/256);; //得到DAC电压值
adcx=temp;
LCD_ShowxNum(94,170+120,temp,1,16,0); //显示电压值整数部分
temp-=adcx;
temp*=1000;
LCD_ShowxNum(110,170+120,temp,3,16,0x80); //显示电压值小数部分
adcx=Get_Adc_Average(ADC_Channel_5,20); //得到ADC转换值
temp=(float)adcx*(3.3/4096); //得到ADC电压值
adcx=temp;
LCD_ShowxNum(94,190+120,temp,1,16,0); //显示电压值整数部分
temp-=adcx;
temp*=1000;
LCD_ShowxNum(110,190+120,temp,3,16,0x80); //显示电压值小数部分
delay_ms(10);
}
}[/mw_shl_code]
谢谢原子哥,已经结局了。还是程序逻辑问题
谢谢了,我现在换一种思路了,
仿真找找问题
一周热门 更多>