今天学习了TI学堂ADC10,写了一个单通道AD转换并通过1602显示的程序
[复制链接]
/********************************************************************* * * *****************************************************************/ #include <intrinsics.h> #include <msp430g2553.h> #include <msp430.h> /*****************************************************
端口定义
****************************************************/ #define LCD_EN_PORT P1OUT //以下2个要设为同一个口 #define LCD_EN_DDR P1DIR #define LCD_RS_PORT P1OUT //以下2个要设为同一个口 #define LCD_RS_DDR P1DIR #define LCD_DATA_PORT P2OUT //以下3个要设为同一个口 #define LCD_DATA_DDR P2DIR //一定要用高4位 #define LCD_RS BIT6 #define LCD_EN BIT7 #define LCD_DATA BIT7|BIT6|BIT5|BIT4 //4位数据线连接模式 /***************************************************
预定义函数
**************************************************/ void LCD_init(void); void LCD_init_first(void); void LCD_en_write1(void); //上升沿使能 void LCD_en_write2(void); //下降沿使能 void LCD_write_command(unsigned char command); void LCD_write_data(unsigned char data); void LCD_set_xy (unsigned char x, unsigned char y); void LCD_write_string(unsigned char X,unsigned char Y, unsigned char *s); void LCD_write_char(unsigned char X,unsigned char Y, unsigned char data); void delay_1ms(void); void delay_nus(unsigned int n); void delay_nms(unsigned int n); void lcd_diaoyong(); void lcd_diaoyong1(); unsigned char LCDBuf1[]={"0123456789"}; //第一行要显示的内容 unsigned char LCDBuf2[]={"DY:.V"}; //第二行要显示的内容 unsigned int j=0,i=0; unsigned int shuzhi=0,qushu=0; /********************************************
主函数
*******************************************/
void main() { WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled ADC10CTL1 = INCH_1; // input A1模拟信号输入选择通道A1即p1.1 ADC10AE0 |= 0x02; // PA.1 ADC option select使p1.1允许AD模拟输入信号 P1DIR |= 0x01; // Set P1.0 to output direction
LCD_init_first(); LCD_init();
TA0CCTL0 = CCIE; // CCR0 1ms中断 TA0CCR0 = 312; TA0CTL = TASSEL_2 + MC_1; // SMCLK, upmode //_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
TA1CCTL0 = CCIE; // CCR0 1ms中断 TA1CCR0 = 312; TA1CTL = TASSEL_2 + MC_1; // SMCLK, upmode _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
while(1) { if(j>100) { shuzhi=qushu*35/10; lcd_diaoyong(); j=0; } } }
/********************************************
lcd子函数
*******************************************/ void lcd_diaoyong() { LCD_write_char(0,0,LCDBuf2[0]); LCD_write_char(1,0,LCDBuf2[1]); LCD_write_char(2,0,LCDBuf2[2]); i=(shuzhi/1000); LCD_write_char(3,0,LCDBuf1);
LCD_write_char(4,0,LCDBuf2[3]);
i=(shuzhi-i*1000)/100; LCD_write_char(5,0,LCDBuf1);
i=(shuzhi/10)%10; LCD_write_char(6,0,LCDBuf1);
LCD_write_char(7,0,LCDBuf2[4]); } /********************************************
lcd子函数1
*******************************************/ void lcd_diaoyong1() { LCD_write_char(0,1,LCDBuf2[0]); LCD_write_char(1,1,LCDBuf2[1]); LCD_write_char(2,1,LCDBuf2[2]); i=(shuzhi/1000); LCD_write_char(3,1,LCDBuf1);
LCD_write_char(4,1,LCDBuf2[3]);
i=(shuzhi-i*1000)/100; LCD_write_char(5,1,LCDBuf1);
i=(shuzhi/10)%10; LCD_write_char(6,1,LCDBuf1);
LCD_write_char(7,1,LCDBuf2[4]); } /******************************************** * 定时器Timer0_A
*******************************************/ #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer0_A (void) { ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start开始转换 qushu=ADC10MEM; j++; __bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } /********************************************
定时器Timer1_A
*******************************************/ #pragma vector=TIMER1_A0_VECTOR __interrupt void Timer1_A (void) { P1OUT ^= 0x01; __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) }
/********************************************
*******************************************/ #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) }
/********************************************
LCD液晶操作函数
*******************************************/
void LCD_init_first(void) //LCD1602液晶初始化函数(热启动) { delay_nms(500); LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出 LCD_EN_DDR|=LCD_EN; //设置EN方向为输出 LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
delay_nms(50); LCD_write_command(0x30); delay_nms(50); LCD_write_command(0x30); delay_nms(5); LCD_write_command(0x30); delay_nms(500);
}
/***************************************** * * LCD1602液晶初始化函数 * ****************************************/ void LCD_init(void) { delay_nms(500); LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出 LCD_EN_DDR|=LCD_EN; //设置EN方向为输出 LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
delay_nms(500);
LCD_write_command(0x28); //4位数据接口 delay_nms(50); LCD_write_command(0x28); //4位数据接口 delay_nms(50); LCD_write_command(0x28); //4位数据接口 delay_nms(50); LCD_en_write2(); delay_nms(50); LCD_write_command(0x28); //4位数据接口 delay_nms(500); LCD_write_command(0x01); //清屏 LCD_write_command(0x0c); //显示开,关光标,不闪烁 LCD_write_command(0x06); //设定输入方式,增量不移位 delay_nms(50);
}
/***************************************** * * 液晶使能上升沿 * ****************************************/
void LCD_en_write1(void) { LCD_EN_PORT&=~LCD_EN; delay_nus(10); LCD_EN_PORT|=LCD_EN; }
/***************************************** * * 液晶使能下降沿 * ****************************************/ void LCD_en_write2(void) { LCD_EN_PORT|=LCD_EN; delay_nus(10); LCD_EN_PORT&=~LCD_EN; }
/***************************************** * * 写指令函数 * ****************************************/ void LCD_write_command(unsigned char command) { delay_nus(16); P2SEL=0x00; LCD_RS_PORT&=~LCD_RS; //RS=0 LCD_en_write1(); LCD_DATA_PORT&=0X0f; //清高四位 LCD_DATA_PORT|=command&0xf0; //写高四位
delay_nus(16); LCD_en_write2(); command=command<<4; //低四位移到高四位 LCD_en_write1(); LCD_DATA_PORT&=0x0f; //清高四位 LCD_DATA_PORT|=command&0xf0; //写低四位 LCD_en_write2(); }
/***************************************** * * 写数据函数 * ****************************************/ void LCD_write_data(unsigned char data) { delay_nus(16); P2SEL=0x00; LCD_RS_PORT|=LCD_RS; //RS=1 LCD_en_write1(); //E上升沿 LCD_DATA_PORT&=0X0f; //清高四位 LCD_DATA_PORT|=data&0xf0; //写高四位 delay_nus(16); LCD_en_write2(); data=data<<4; //低四位移到高四位 LCD_en_write1(); LCD_DATA_PORT&=0X0f; //清高四位 LCD_DATA_PORT|=data&0xf0; //写低四位 LCD_en_write2(); }
/***************************************** * * 写地址函数 * ****************************************/ void LCD_set_xy( unsigned char x, unsigned char y ) { unsigned char address; if (y == 0) address = 0x80 + x; else address = 0xc0 + x; LCD_write_command( address); }
/***************************************** * *LCD在任意位置写字符串,列x=0~15,行y=0,1 * ****************************************/ void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) { LCD_set_xy( X, Y ); //写地址 while (*s) //写显示字符 { LCD_write_data( *s ); s++; } }
/***************************************** * * LCD在任意位置写字符,列x=0~15,行y=0,1 * ****************************************/
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) { LCD_set_xy( X, Y ); //写地址 LCD_write_data( data); }
/***************************************** * * 1us延时函数 * ****************************************/
void delay_1us(void) { asm("nop"); }
/***************************************** * * N us延时函数 * ****************************************/ void delay_nus(unsigned int n) { unsigned int i; for (i=0;i<n;i++) delay_1us(); }
/***************************************** * * 1ms延时函数 * ****************************************/ void delay_1ms(void) { unsigned int i; for (i=0;i<1140;i++); }
/***************************************** * * N ms延时函数 * ****************************************/ void delay_nms(unsigned int n) { unsigned int i=0; for (i=0;i<n;i++) delay_1ms(); }
这个程序大家可以参考一下,结果是对的,但我觉得方法可能有点问题。
|