DOC

avr 1602

By Lauren Armstrong,2014-06-11 10:42
23 views 0
avr 1602

ATMEGA+PROTEUS仿真1602的程序

    #include

    #include

    #define uint unsigned int

    #define uchar unsigned char #pragma data:code //把数组放入程序空间

    const uchar table[]="I LIKE AVR !"; const uchar table1[]="my first avr lcd";

    void delay(uint ms)

    {

     uint i,j;

     for(i=0;i

     {

     for(j=0;j<1141;j++);

     }

    }

    void write_1602_com(uchar com) {

     PORTD&=~BIT(0);//rs=low

     PORTC&=~BIT(1);//RW

     PORTC=com;

     PORTD|=BIT(2);//EN=1

     delay(5);

     PORTD&=~BIT(2);//EN=0;

    }

    void write_1602_data(uchar dat) {

     PORTD|=BIT(0);//rs=low

     PORTC&=~BIT(1);//RW

     PORTC=dat;

     PORTD|=BIT(2);//EN=1

     delay(5);

     PORTD&=~BIT(2);//EN=0;

    }

    void main()

    {

     uint i,j;

     DDRD=0XFF;

     DDRC=0XFF;//配置为输出,D0-D7

     DDRD|=BIT(0)|BIT(1)|BIT(2);//培植输出

     PORTD|=BIT(2); //EN=0

     //显示光标

     write_1602_com(0x38);

     delay(5);

     write_1602_com(0x01);//clear lce

     delay(5);

     write_1602_com(0x0f);//光标闪

     delay(5);

     write_1602_com(0x06);

     delay(5);

     /*

     write_1602_com(0x80+0);//addr

     for(i=0;i<12;i++)

     {

     write_1602_data(table[i]);

     delay(5);

     }

     write_1602_com(0x80+0x40);//调试过程中开始把这里直接写为10进制的40,

    导致地址不正确,无法显示

     for(i=0;i<16;i++)

     {

     write_1602_data(table1[i]);

     delay(5);

     }

     */

     write_1602_com(0x80+0x10);//addr

     for(i=0;i<12;i++)

     {

     write_1602_data(table[i]);

     delay(5);

     }

     write_1602_com(0x80+0x50);//

     for(i=0;i<16;i++)

     {

     write_1602_data(table1[i]);

     delay(5);

     }

     for(i=0;i<24;i++)//要移动24位才够 刚开始移动16,很多字符没显示

     {

     write_1602_com(0x18);

     delay(50);

     }

}

#include//头文件

    #include

    #define uchar unsigned char//宏定义 #define uint unsigned int

    #prama data: code

    const uchar table[]="Hello!BeiJing!";//第一行显示

    const uchar table1[]="QQ:523969143";//第二行显示

    void delay(uint ms) //延时函数

     {

     uint i,j;

     for(i=0;i

     for(j=0;j<1411;j++);

     }

     void write_com(uchar com) //写指令

     {

     PORTD&=~BIT(4);

     PORTD&=~BIT(5);

     PORTB=com;

     PORTD|=BIT(6);

     delay(5);

     PORTD&=~BIT(6);

     }

     void write_dat(uchar dat) //写数据

     {

     PORTD|=BIT(4);

     PORTD&=~BIT(5);

     PORTB=dat;

     PORTD|=BIT(6);

     delay(5);

     PORTD&=~BIT(6);

     }

     void lcd_init() //初始化

     {

     uchar i;

     write_com(0x01);

     delay(5);

     write_com(0x38);

     delay(5);

     write_com(0x06);

     delay(5);

     write_com(0x0c);

     delay(5);

     write_com(0x80+0x11);//第一行显示位置

     delay(5);

     for(i=0;i<14;i++)

     {

     write_dat(table[i]); //显示第一行

     delay(5);

     }

     write_com(0x80+0x53);//第二显示位置

     for(i=0;i<12;i++)

     {

     write_dat(table1[i]); //显示第二行

     delay(5);

     }

     for(i=0;i<16;i++)

     {

     write_com(0x18); //整屏左移

     delay(300);

     }

     }

     void main()

     {

     DDRD|=BIT(4)|BIT(5)|BIT(6); //把方向寄存器D4D5D6设置为输出

     DDRB=0XFF; //把方向寄存器B设置为输出

     lcd_init(); //显示LCD

     }

    Avr用查询方式读取adc的值 void adc_init(unsigned char adc_channel)

    {

     ADCSRA = 0x00;

     ACSR = 0x80;

     ADMUX = (0<

     ADCSRA = (1<

    |(1<

    }

unsigned int GetAdcValue(unsigned char adc_channel)

    {

     unsigned int adc_value=0,temp=0;

     unsigned char i=0;

     adc_init(adc_channel);

     delay_nms(30);

     for(i=0;i<2;i++)

     {

     ADCSRA|=(1<

     CLI();

     while((ADCSRA & 0x10)==0);

     SEI();

     ADCSRA |= (1<

     temp = ADCL;

     temp |= (int)ADCH<<8;

     }

     for(i=0;i<5;i++)

     {

     ADCSRA|=(1<

     CLI();

     while((ADCSRA & 0x10)==0);

     SEI();

     ADCSRA |= (1<

     temp = ADCL;

     temp |= (int)ADCH<<8;

     adc_value = adc_value + temp;

     }

     adc_value = adc_value/5 ;

     return adc_value;

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com