已验证 可行
#include"msp430g2553.h" #define uint unsigned int #define uchar unsigned char uint c[32],b; uint i=0; float n,v; uint d1,d2,d3,d4,d5; uchar a1[]={"12864 "}; uchar a2[]={"液晶显示 "}; uchar a3[]={"哈喽 "}; #define SID BIT1 #define SCLK BIT2 #define CS BIT0 #define SID_1 P2OUT |= SID #define SID_0 P2OUT &= ~SID #define SCLK_1 P2OUT |= SCLK #define SCLK_0 P2OUT &= ~SCLK #define CS_1 P2OUT |= CS #define CS_0 P2OUT &= ~CS void send_command(uchar command_data)//写命令 { uchar i; uchar i_data; i_data=0xf8; CS_1; SCLK_0; for(i=0;i<8;i++) { SID_0; if(i_data&0x80) SID_1 ; _delay_cycles(100); SCLK_0; SCLK_1; _delay_cycles(10); i_data<<=1; } i_data=command_data; i_data&=0xf0; for(i=0;i<8;i++) { SID_0; if(i_data&0x80) SID_1 ; _delay_cycles(100); SCLK_0; SCLK_1; _delay_cycles(10); i_data<<=1; } i_data=command_data; i_data<<=4; for(i=0;i<8;i++) { SID_0; if(i_data&0x80) SID_1 ; _delay_cycles(100); SCLK_0; SCLK_1; _delay_cycles(10); i_data<<=1; } CS_0; _delay_cycles(100); } void send_data(uchar command_data)//写数据 { uchar i; uchar i_data; i_data=0xfa; CS_1; SCLK_0; for(i=0;i<8;i++) { SID_0; if(i_data&0x80) SID_1 ; _delay_cycles(100); SCLK_0; SCLK_1; _delay_cycles(10); i_data<<=1; } i_data=command_data; i_data&=0xf0; for(i=0;i<8;i++) { SID_0; if(i_data&0x80) SID_1 ; i_data<<=1; _delay_cycles(100); SCLK_0; SCLK_1; _delay_cycles(10); } i_data=command_data; i_data<<=4; for(i=0;i<8;i++) { SID_0; if(i_data&0x80) SID_1 ; i_data<<=1; _delay_cycles(100); SCLK_0; SCLK_1; _delay_cycles(10); } CS_0; _delay_cycles(100); }
void lcd_init(void)//液晶初始化 { send_command(0x30); _delay_cycles(50); send_command(0x0c); _delay_cycles(50); send_command(0x01); _delay_cycles(50); } void display_s1()//第一行 { uchar a; send_command(0x80); for(a=0;a<16;a++) { send_data(a1[a]); } } void display_s2()//第二行 { uchar a; send_command(0x90); for(a=0;a<16;a++) { send_data(a2[a]); } } void display_s3()//第三行 { uchar a; send_command(0x88); for(a=0;a<16;a++) { send_data(a3[a]); } } void display_ad()//第四行 { send_command(0x98); send_data('D'); send_data('V'); send_data(d1+0x30); send_data('.'); send_data(d2+0x30); send_data(d3+0x30); send_data(d4+0x30); } void jisuan(float x) { uint y; y=x*1000; d1=0;d2=0;d3=0;d4=0;d5=0; d1=y/1000; d2=y%1000; d3=d2%100; d4=d3%10; d2=d2/100; d3=d3/10; }
/* * main.c */ void main(void) { WDTCTL=WDTHOLD+WDTPW; P2SEL=0x00; P2DIR = BIT1 + BIT2 + BIT0+BIT3; P2OUT&=~BIT3; ADC10CTL1|= INCH_4 + CONSEQ_2; // 采样触发源选择ADC10SC 输入通道A4 4分频 单通道多次转换 ADC10CTL0=SREF_1+REFON+REF2_5V+MSC+ADC10IE+ADC10ON;// 参考电压选择基准电压 打开内部参考电压发生器 参考电压选择2.5V 一次触发连续执行 开启AD功能 ADC10AE0|=0X10;//开启通道4 (使能) ADC10DTC1 = 0x20;//转换32次 lcd_init(); //液晶初始化 _delay_cycles(10); while(1) { ADC10CTL0&=~ENC;//关闭采样使能 while(ADC10CTL1&BUSY);//判断是否有采样或转换动作 ADC10SA = (unsigned int)c; ADC10CTL0|=ADC10SC+ENC;//开始采样 _bis_SR_register(LPM0_bits+GIE);//关闭cpu,开启中断 _NOP(); _NOP(); } } #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { uint j; b=0; for(j=0;j<32;j++) { b+=c[j]; } b=b/32; v=(b*2.5)/1023; jisuan(v); display_s1(); display_s2(); display_s3(); display_ad(); _delay_cycles(10); __bic_SR_register_on_exit(CPUOFF);
}
[ 本帖最后由 zhanghua1209 于 2012-7-24 00:47 编辑 ]
|