四极管:AD转换之AVR AD TLC 5510

来源:互联网 发布:淘宝定金和尾款退款 编辑:程序博客网 时间:2024/05/16 10:02

主函数:

四极管:main.c

#ifndef MAIN_C#define MAIN_C#include <avr/io.h>#include <util/delay.h>#include "1602driver.c"#include "TLC_5510.c"uint8_t num[]="0123456789";int main(void){    DDRB=0XFF;PORTB=0XFF;    Init_1602();     uint8_t   adc;uint32_t  adc_0;Displaypstr(0,0,"ad = ");    while(1)     {                  adc = Tlc_5510();adc_0 = (adc*5000)>>8;        Displaychar(0,1,num[adc/100]);Displaychar(1,1,num[adc%100/10]);Displaychar(2,1,num[adc%10]);Displaychar(3,0,num[adc_0/1000]);Displaychar(4,0,num[adc_0%1000/100]);Displaychar(5,0,num[adc_0%100/10]);Displaychar(6,0,num[adc_0%10]);_delay_ms(100);     }}#endif


TLC_5510驱动:(TLC_5510.c)

#ifndef  TLC_5510_C#define  TLC_5510_C/*********************************** 函数功能:8位高速AD并行转换(TLC5510)           使用环境:    硬件: MCU: ATmega32L                       F_CPU = 16000000                      外部:16000000    编译环境:WinAVR-20080610 包含子程序:#include <util/delay.h>   日期:2009年7月13日 作者:杨琦 修改者:    (日期:  )   ***********************************/#include <util/delay.h>#define DATA_IN()   { DDRA=0X00;}#define DATA_PIN   PINA  #define  OE_0()    {DDRB|=(1<<0);PORTB&=~(1<<0);}#define  OE_1()    {DDRB&=~(1<<0);PORTB|=(1<<0);}#define  CLK_0()   {DDRB|=(1<<1);PORTB&=~(1<<1);}#define  CLK_1()   {DDRB&=~(1<<1);PORTB|=(1<<1);}/****************************************** 函数名:Tlc_5510(void)** 功能  :5510驱动** 子函数:_delay_ms()** 返回值 :uint8_t****************************************/uint8_t   Tlc_5510(void){    uint8_t AD_value = 0;DATA_IN();    OE_0();_delay_ms(1);    CLK_1(); _delay_ms(1);    CLK_0();_delay_ms(1);        AD_value = DATA_PIN;     return AD_value;}#endif

四极管:显示(1602driver.c)

/*********************************** 函数功能:1602 液晶显示两行数字和字符 函数说明:四线方式,数据输出线:DATA_PORT           使用环境:    硬件: MCU: ATmega32L                       F_CPU = 16000000                      外部:16000000  接线方法:3跟数据线:PC1-RS   PC2-RW  PC3-E           4跟数据线:PC   编译环境:WinAVR-20080610 显示说明:           显示字符函数:             Displaychar(unsigned char x,unsigned char y, unsigned char wdata)            (x为横坐标   y为坐标 wdata为所要显示的东西) 显示字符串函数:     Displaypstr(unsigned char x,unsigned char y,unsigned char *str)            (x为横坐标   y为坐标  wdata为所要显示的东西)   坐标定位说明:X:0-15                Y:0-1 包含文件 :MyBit.h  作者:杨琦   日期:2009年7月13日 修改者:    (日期:  )   ***********************************/  //管脚定义  #define DATA_PORT PORTC#define RS_0() {DDRC |=(1<<1);PORTC &=~(1<<1);}       #define RS_1() {DDRC |=(1<<1);PORTC |= (1<<1);}#define RW_0() {DDRC |=(1<<2);PORTC &=~(1<<2);}#define RW_1() {DDRC |=(1<<2);PORTC |= (1<<2);}#define E_0()  {DDRC |=(1<<3);PORTC &=~(1<<3);} #define E_1()  {DDRC |=(1<<3);PORTC |= (1<<3);} #define budy_in()   {DDRC&=~(1<<7);PORTC|=(1<<7);}#define budy_high    PINC&(1<<7)#define busy_out()   {DDRC|=(1<<7);}//注:读忙函数需对输出口最高位进行设置和判断/****************************************** 函数名:readbusy** 功能  :读忙函数** 子函数:无** 变量  :无----------------------------------------****************************************/void readbusy(void){ //检测忙位改为输入budy_in();//拉高    RW_1();RS_0();E_1();while(budy_high);//等待为低电平   ????????@@@@@@E_0();busy_out(); }/****************************************** 函数名:sentbyte** 功能  :发送一节数** 子函数:readbusy()** 变量  :数据或命令 tybe=1数据 0命令----------------------------------------****************************************/void sentbyte(unsigned char i,unsigned char tybe){    unsigned char temp;readbusy();    if(tybe == 1){    RW_0();        RS_1();    }if(tybe == 0){    RW_0();RS_0();}temp = i&0xf0;DATA_PORT &=0x0f;    DATA_PORT |=temp;    E_1();    E_0(); temp = (i<<4)&0xf0;DATA_PORT &=0X0f;DATA_PORT |=temp;E_1();    E_0(); }/****************************************** 函数名:Init_1602()** 功能  :初始化1602液晶显示** 变量  :无----------------------------------------****************************************/void Init_1602(void){    DDRC = 0xff;    sentbyte(0x30,0);    sentbyte(0x30,0);sentbyte(0x30,0);sentbyte(0x02,0);    sentbyte(0x28,0);   //设置工作方式:4位总线字符两行0x28,8位总线字符两行0x38sentbyte(0x01,0);  //清屏sentbyte(0x02,0);   //数据指针清零sentbyte(0x0c,0);   //开光标,但光标不闪sentbyte(0x06,0);   //写字符后光标加一sentbyte(0x01,0);   //清屏}/****************************************** 函数名:Locate_xy()** 功能  :写地址** 变量  :x,y----------------------------------------****************************************/void Locate_xy(unsigned char x,unsigned char y ){    unsigned char addr=0;if(y == 0)addr=0x80+x;if(y == 1)addr=0xc0+x;    sentbyte(addr,0);}/****************************************** 函数名:Displaychar()** 功能  :1602液晶显示一个字符** 变量  :x,y,wdata----------------------------------------****************************************/void Displaychar(unsigned char x,unsigned char y,  unsigned char wdata){    Locate_xy(x,y);       sentbyte(wdata,1);}/****************************************** 函数名:Displaypstr()** 功能  :1602液晶显示一个字符串** 变量  :x,y,*str----------------------------------------****************************************/void Displaypstr(unsigned char x,unsigned char y,unsigned char *str){    unsigned int i=0;Locate_xy(x,y);   while(str[i]!='\0'){    sentbyte(str[i++],1);    x++;    if(x>15)    {   x=0;   y++;   if(y==2)y=0;   Locate_xy(x,y);   }}    }

转载请注明出处。作者:四极管。广西师范大学 电子工程学院大学生科技创新基地 邮箱: yangxingbo-0311@163.com。


原创粉丝点击