| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4364 人关注过本帖
标题:本人新手,有一段代码看不懂,求大神注解(这是关于avr单片机所设的电压监测 ...
只看楼主 加入收藏
天涯请问
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-12-31
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
本人新手,有一段代码看不懂,求大神注解(这是关于avr单片机所设的电压监测系统)
#include <iom16v.h>
#include <macros.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_EN         BIT(PC3)
#define LCD_RS         BIT(PC2)

uchar title[]={"hello!"};
uchar title1[]={"key:"};

uchar key_flag;
unsigned  data_temp;
uchar xians[]="0123456789";

  
void delay(unsigned int i)
{
    unsigned int a, b;
    for (a = 1; a < i; a++)
    {
        for (b = 1; b<2000; b++);
    }
}
void write_com(uchar com)
{
       PORTD&=~BIT(3);
       PORTD&=~BIT(4);
       PORTB=com;
       PORTD|=BIT(6);
       delay(3);
       PORTD&=~BIT(6);
}
void write_dat(uchar dat)
{     
       PORTD|=BIT(3);
       PORTD&=~BIT(4);
       PORTB=dat;
       PORTD|=BIT(6);
       delay(3);
       PORTD&=~BIT(6);
}
void Uart_Init(void)
 { UCSRA = 0x02;  
  UCSRB = 0x18;  
  UCSRC = 0x06;  
  UBRRH = 0x00;
  UBRRL = 12;  
  }


void Uart_Transmit(unsigned char i)
{ while (!(UCSRA & (1<<UDRE)));  
 UDR = i;  
 }

uint mega16_ad()
{
    uint addata;
    DDRA&=~BIT(PA0);
    PORTA&=~BIT(PA0);
   
    ADMUX=0xC0;
    ADCSRA=0X80;
   
    ADCSRA|=BIT(ADSC);
    while(!(ADCSRA&(BIT(ADIF))));
   
   
    addata=ADCL;
    addata=addata+ADCH*256;
    return addata;
}

int main(void)

{   
 uint ad;
 float adc0;
    uchar k,j,k1,k2,k3;
   
   DDRD = 0x22;
     PORTD = 0xFF;
     
    Uart_Init();
    while(1)
    {
        ad=mega16_ad();
        adc0=ad*5.0/1024;
        k1=(int)adc0;
        k2=(int)((adc0-k1)*10);
        k3=(int)((adc0-k1-k2*0.1)*100);
      DDRB=0XFF;
      DDRD|=BIT(3)|BIT(4)|BIT(6);
      PORTD&=~BIT(6);
      write_com(0X38);
      delay(9);
      write_com(0X01);
      delay(9);
      write_com(0X0C);
      delay(9);
      write_com(0X06);
      delay(9);
      if (k1>=4)
        PORTD&=~BIT(5);
        
        else
         PORTD|=BIT(5);
 
      while(1)
     {
       write_com(0X80+0X00);
       delay(5);
       write_dat(xians[k1]);
       delay(5);
        write_dat(0x2e);
       delay(5);
        write_dat(xians[k2]);
       delay(5);
       write_dat(xians[k3]);
       delay(5);
       write_dat('V');
       delay(5);
       Uart_Transmit(xians[k1]);
       Uart_Transmit(0x2e);
       Uart_Transmit(xians[k2]);
       Uart_Transmit(xians[k3]);
       Uart_Transmit('V');
       delay(2);
    }
    return 0;}
}
搜索更多相关主题的帖子: include 单片机 hello title 
2013-12-31 15:58
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
关注

DO IT YOURSELF !
2014-01-01 23:02
小xiong
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:388
专家分:1722
注 册:2013-2-8
收藏
得分:10 
#include <iom16v.h>
 #include <macros.h>
 #include <math.h>
 #define uchar unsigned char
 #define uint unsigned int
 #define LCD_EN         BIT(PC3)
 #define LCD_RS         BIT(PC2)
 
uchar title[]={"hello!"};
 uchar title1[]={"key:"};
 
uchar key_flag;
 unsigned  data_temp;
 uchar xians[]="0123456789";
 
  
 void delay(unsigned int i)
 {
     unsigned int a, b;
     for (a = 1; a < i; a++)
     {
         for (b = 1; b<2000; b++);
     }
 }
 void write_com(uchar com)
 {
        PORTD&=~BIT(3);
        PORTD&=~BIT(4);
        PORTB=com;
        PORTD|=BIT(6);
        delay(3);
        PORTD&=~BIT(6);
 }
 void write_dat(uchar dat)
 {     
        PORTD|=BIT(3);
        PORTD&=~BIT(4);
        PORTB=dat;
        PORTD|=BIT(6);
        delay(3);
        PORTD&=~BIT(6);
 }
 void Uart_Init(void)
 { UCSRA = 0x02;  
   UCSRB = 0x18;  
   UCSRC = 0x06;  
   UBRRH = 0x00;
  UBRRL = 12;  
   }
 

void Uart_Transmit(unsigned char i)
{ while (!(UCSRA & (1<<UDRE)));  
 UDR = i;  
 }
 
uint mega16_ad(void)//读取片内ad值
 {
     uint addata;
     DDRA&=~BIT(PA0);
     PORTA&=~BIT(PA0);
     
    ADMUX=0xC0;
     ADCSRA=0X80;
     
    ADCSRA|=BIT(ADSC);
     while(!(ADCSRA&(BIT(ADIF))));
     
   
    addata=ADCL;
     addata=addata+ADCH*256;
     return addata;
 }
 

int main(void)
 
{   
 uint ad;
 float adc0;
     uchar k,j,k1,k2,k3;
   
    DDRD = 0x22;
     PORTD = 0xFF;//设置GPIO
      
     Uart_Init();//串行初始化
     while(1)
     {
        ad=mega16_ad();
         adc0=ad*5.0/1024;//数据处理
         k1=(int)adc0;
         k2=(int)((adc0-k1)*10);
         k3=(int)((adc0-k1-k2*0.1)*100);
       DDRB=0XFF;
       DDRD|=BIT(3)|BIT(4)|BIT(6);
       PORTD&=~BIT(6);
       write_com(0X38);//后面是液晶的显示处理
       delay(9);
       write_com(0X01);
       delay(9);
       write_com(0X0C);
       delay(9);
       write_com(0X06);
       delay(9);
       if (k1>=4)
         PORTD&=~BIT(5);
         
         else
          PORTD|=BIT(5);

       while(1)
      {
       write_com(0X80+0X00);
        delay(5);
        write_dat(xians[k1]);
        delay(5);
         write_dat(0x2e);
        delay(5);
         write_dat(xians[k2]);
        delay(5);
        write_dat(xians[k3]);
        delay(5);
        write_dat('V');
        delay(5);
        Uart_Transmit(xians[k1]);
        Uart_Transmit(0x2e);
        Uart_Transmit(xians[k2]);
        Uart_Transmit(xians[k3]);
        Uart_Transmit('V');
        delay(2);
     }
     
     }return 0;
 }
2014-01-04 10:17
快速回复:本人新手,有一段代码看不懂,求大神注解(这是关于avr单片机所设的电 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027478 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved