#include "msp430g2553.h"
#include "12864.h"
unsigned int i,Vo;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL0 = SREF_0 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE + REF2_5V;
//__enable_interrupt(); // Enable interrupts.
//TACCR0 = 30; // Delay to allow Ref to settle
//TACCTL0 |= CCIE; // Compare-mode interrupt.
//TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode.
//LPM0; // Wait for delay.
//TACCTL0 &= ~CCIE; // Disable timer Interrupt
//__disable_interrupt();
ADC10CTL1 = INCH_3; // input A3
ADC10AE0 |= 0x08; // PA.3 ADC option select
__enable_interrupt();
Ini_Lcd();
for (;;)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
//__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exi
Vo=ADC10MEM/1024.0*35670;
lcd_pos(1,0);
Disp_HZ("The volt is",6);
lcd_pos(2,3);
Disp_SZ(Vo/10000);
Disp_HZ(".",1);
lcd_pos(2,4);
Disp_SZ(Vo/1000);
Disp_SZ(Vo/100);
lcd_pos(2,5);
Disp_SZ(Vo/10);
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
//#pragma vector=TIMER0_A0_VECTOR
//__interrupt void ta0_isr(void)
//{
// TACTL = 0;
// LPM0_EXIT; // Exit LPM0 on return
//}
#include
#define BIT(x) (1 < (x))
#define uint unsigned int
#define uchar unsigned char
uchar LCD_table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};//0123456789
void Send(uchar type,uchar transdata);
#define cyCS 0 //P1.0,片选信号
#define cySID 1 //P1.1,串0行数据
#define cyCLK 2 //P1.2,同步时钟
#define cyPORT P1OUT
#define cyDDR P1DIR
void delay_1ms()
{
uint i;
for(i=0;i<1140;i++);
}
void delay_nms(uint n)
{
uint i = 0;
for(i = n;i > 0;i--)
delay_1ms();
}
void Ini_Lcd(void)
{
cyDDR |= BIT(cyCLK) + BIT(cySID) + BIT(cyCS); //相应的位端口设置为输出
delay_nms(600); //延时等待液晶完成复位
Send(0,0x30);
delay_nms(10);
Send(0,0x02);
delay_nms(5);
Send(0,0x0c);
delay_nms(5);
Send(0,0x01);
delay_nms(500);
Send(0,0x06);
delay_nms(10);
}
void Send(uchar type,uchar transdata)//0-写控制命令,1-写数据
{
uchar firstbyte = 0xf8;
uchar temp;
uchar i,j = 3;
if(type) firstbyte |= 0x02;
cyPORT |= BIT(cyCS);
cyPORT &= ~BIT(cyCLK);
while(j > 0)
{
if(j == 3) temp = firstbyte;
else if(j == 2) temp = transdata&0xf0;
else temp = (transdata < 4) & 0xf0;
for(i = 8;i > 0;i--)
{
if(temp & 0x80) cyPORT |= BIT(cySID);
else cyPORT &= ~BIT(cySID);
cyPORT |= BIT(cyCLK);
temp <= 1;
cyPORT &= ~BIT(cyCLK);
}
//三个字节之间一定要有足够的延时,否则易出现时序问题
if(j == 3) delay_nms(1);
else delay_nms(1);
j--;
}
cyPORT &= ~BIT(cySID);
cyPORT &= ~BIT(cyCS);
}
void lcd_pos(uchar x,uchar y)//定位
{
uchar pos;
switch(x)
{
case 1:pos=0x80;break;
case 2:pos=0x90;break;
case 3:pos=0x88;break;
case 4:pos=0x98;break;
default:pos=0x80;
}
pos += y;
Send(0,pos);
}
void Disp_HZ(const uchar*pt,uchar num)//显示汉字
{
uchar i;
for(i = 0;i < (num*2);i++)
Send(1,*(pt++));
}
void Disp_SZ(uchar shuzi)//显示一个数字
{
Send(1,LCD_table[shuzi]);
}
#ifndef LCD12864_H_
#define LCD12864_H_
#define uint unsigned int
#define uchar unsigned char
extern uchar LCD_table[];//0123456789
extern void delay_1ms();
extern void delay_nms(uint n);
extern void Ini_Lcd(void);
extern void Send(uchar type,uchar transdata);//0-写控制命令,1-写数据
extern void lcd_pos(uchar x,uchar y);//定位
extern void Disp_HZ(const uchar * pt,uchar num);//显示汉字
extern void Disp_SZ(uchar shuzi);//显示一个数字
#endif