标题:本人新手,有一段代码看不懂,求大神注解(这是关于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: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-425955-1-1.html




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

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