我根据AD转换值产生PWM波,调试出问题

2019-03-24 20:47发布

//ICC-AVR application builder : 2011-8-26 下午 08:01:50
// Target : M16
// Crystal: 7.3728Mhz #include <iom16v.h>
#include <macros.h>
#define   LCD_RS  PC7
#define   LCD_RW  PC6
#define   LCD_EN  PC5
#define   LCD_DD  PD
#define uchar unsigned char
#define uint unsigned int
unsigned char temp[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char temp1000,temp100,temp10,temp1;
void PWM_init(void)
{
  DDRB=0xFF;             //PB口输出高电平,LED全处于熄灭状态
  PORTB=0xFF;
  TCCR0=0x63;            //0b01100001,PWM相位修正模式,无64分频,在升序计数时发
                         //生比较匹配将清零OC0,降序计数时发生比较匹配将置位OC0
 
}
void PWM_out(unsigned char m)
{
  if(m<2)
   OCR0=100;
  else
   OCR0=255;
 }
unsigned int ADC_Init(void)
{
    unsigned int dat;
    DDRA&=~(1<<PA0);
    PORTA&=~(1<<PA0);
    ADMUX=0;//ADC多工道寄存器,参考外部电压,右对齐,选取PA第一通道
    ADCSRA=0x80;//A通道控制和状态寄存器,取消开始转换,中断标志,自动触发,取消中断使能ADCSR在ICC中可以
    ADCSRA|=1<<ADSC;//开始转换指令
    while(!(ADCSRA&(1<<ADIF)));//中断使能,检测中断使能位同时为高说明已转换完
    ADCSRA |= (1 << ADIF);   //清零ADC中断标志位
    dat=ADCL;//已定义右对齐,先读低8位
    dat=dat+ADCH*256;//在读高8位
    return dat;
}
void delayms(unsigned int ms)
{
   unsigned int j;
   while(ms--)
   {
     for(j=0;j<1141;j++);
   }
}
void  LCD1602_Port_Init(void)
{
 /*LCD1602数据端口设置*/
 DDRD=0xff;//数据口全为输出
 PORTD=0;//输出低电平
  
 /*LCD1602控制端口设置*/
 DDRC|= (1 <<LCD_RS) | (1 <<LCD_RW) | (1 <<LCD_EN);
 PORTC &= ~((1 <<LCD_RS) | (1 <<LCD_RW) | (1 <<LCD_EN));       
} /*LCD1602初始化定义*/
void LCD1602_Init(void)
{
   delayms(400);
   LCD1602_Write_Command(0x38);//选择显示模式
   delayms(15);
   LCD1602_Write_Command(0x38);//选择显示模式
   delayms(15);
   LCD1602_Write_Command(0x38);//选择显示模式
   delayms(15);
   LCD1602_Write_Command(0x0c);//让显示屏的光标闪烁
    delayms(10);
   LCD1602_Write_Command(0x06);//写完一个字符,显示地址指针自加一
   delayms(10);
   LCD1602_Write_Command(0x01);
   delayms(10);
} /*LCD1602写指令函数*/
void LCD1602_Write_Command(unsigned char command)
{
  PORTC&=~(1<<LCD_RS);
  PORTC&=~(1<<LCD_RW);
  PORTD=command;
  PORTC|=1<<LCD_EN;
  delayms(5);
  PORTC&=~(1<<LCD_EN);//1602下降沿送数据
} /*LCD1602写数据指令函数*/
void LCD1602_Write_Data(unsigned char dat)
{
  PORTC|=1<<LCD_RS;
  PORTC&=~(1<<LCD_RW);
  PORTD=dat;
  PORTC|=1<<LCD_EN;
  delayms(5);
  PORTC&=~(1<<LCD_EN);//1602下降沿送数据
} /*LCD1602指定一个位置显示一个字符*/
void LCD1602_DisplayOneChar(unsigned char x,unsigned char y,unsigned char dat)
{
  unsigned char address;
  if(y==1)
     address=0x80+x;
   else
     address=0x0c+x;//算出地址码
 LCD1602_Write_Command(address);//找到显示的位置
 LCD1602_Write_Data(dat);//显示要显示的字符
} /*LCD1602指定位置显示一个字符串*/
void LCD1602_DisplayStringChar(unsigned char x,unsigned char y,unsigned char *s)
{
  unsigned char address,i;
  uint lengh;
  if(y==1)
     address=0x40+x;
  else
     address=0xc0+x;
  LCD1602_Write_Command(address);
  lengh= (uint)strlen(s);//调用字符串函数算出字符的长度,并赋值给lengh
  for(i=0;i<lengh;i++)
  {
   LCD1602_Write_Data(*s);//显示一个字符
   s++;
  }
} /*LCD1602显示自定义字符*/
void LCD1602_DisplayMyChar(char xx,char yy,unsigned char *MyChar,unsigned char saveto)//*作用指向对象的地址
{
  unsigned char add=(saveto<<3)|0x40;
  unsigned char i,t;
  t=*(MyChar+0);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第一行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+1);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第二行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+2);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第三行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+3);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第四行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+4);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第五行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+5);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第六行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+6);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第七行
  LCD1602_Write_Data(t);//写显示数据
  t=*(MyChar+7);
  LCD1602_Write_Command(add);//选中xx,yy位置5*8点阵中的第八行
  LCD1602_Write_Data(t);//写显示数据
 
  for(i=0;i<8;i++)
  {
   LCD1602_Write_Command(add+i);
   LCD1602_Write_Data(*(MyChar+i));
 
  }
  LCD1602_DisplayOneChar(xx,yy,saveto);
} void main(void)
{
   uint ad_data;
   PWM_init();
   LCD1602_Port_Init();
   LCD1602_Init();
   LCD1602_DisplayStringChar(0,2,"m16 ADC tranform");
   SEI();
 while(1)
 {
  ad_data=ADC_Init();
  ad_data=(5000*(long)ad_data)/1023;
  PWM_out(ad_data);
  temp1000=ad_data/1000;
  temp100=ad_data%1000/100;
  temp10=ad_data%1000%100/10;
  temp1=ad_data%1000%100%10;
  delayms(20);
  LCD1602_DisplayOneChar(1,1,temp[temp1000]);
  LCD1602_DisplayOneChar(2,1,temp[temp100]);
  LCD1602_DisplayOneChar(3,1,temp[temp10]);
  LCD1602_DisplayOneChar(4,1,temp[temp1]);
  
  }
 
} 这是我写的ad转换加PWM程序,用ICCV7编译出现如下问题:iccv7avrinimakew -f LCD1602.mak
    iccavr -c -e -D__ICC_VERSION=722 -DATMega16  -l -g -MLongJump -MHasMul -MEnhanced  main.c
!W E:ICCprojectLCD1602+AD程序main.c(67):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!W E:ICCprojectLCD1602+AD程序main.c(83):[warning] declaration of `LCD1602_Write_Command' does not match previous declaration at E:ICCprojectLCD1602+AD程序main.c(67)
!W E:ICCprojectLCD1602+AD程序main.c(125):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
    iccavr -o LCD1602 -g -e:0x4000 -ucrtatmega.o -bfunc_lit:0x54.0x4000 -dram_end:0x45f -bdata:0x60.0x45f -dhwstk_size:30 -beeprom:0.512 -fihx_coff -S2 @LCD1602.lk   -lcatmega
Device 9% full.
Done. Wed Aug 31 20:45:56 2011
请大哥大姐指导下啊,这是什么问题 这是我的附件: LCD1602+AD程序.rar (129.48 KB, 下载次数: 26) 2011-8-31 20:51 上传 点击文件名下载附件 [ 本帖最后由 guowj513 于 2011-8-31 20:51 编辑 ] 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
3条回答
heguoming123
2019-03-25 11:24
楼主这个是数控电源程序么

一周热门 更多>