📄 msp430demo.c
字号:
#include <msp430x44x.h>
#include <math.h>
// These need to be global in this example. Otherwise, the
// compiler removes them because they are not used
static unsigned int results[3];
float resultsum[3]={0,0,0};
unsigned char key ;
// P2.6 SCL
// P2.7 SDA
#define SCL_1 P2OUT |= BIT6; //SCL=1
#define SCL_0 P2OUT &= ~BIT6; //SCL=0
#define SDA_1 P2OUT |= BIT7; //SDA=1
#define SDA_0 P2OUT &= ~BIT7; //SDA=0;
#define DIR_OUT P2DIR |= BIT7; //OUTPUT
#define DIR_IN P2DIR &= ~BIT7; //INPUT
#define SDA_IN ((P2IN >> 7) & 0x01); //Read SDA
#define key1 0x01;
#define key2 0x02;
char* LCD = LCDMEM;
char templcd;
char digitud[13] =
{
0x7D, // "0" LCD segments a+b+c+d+e+f
0x60, // "1" DP c b a , d e g f
0x3E, // "2"
0x7A, // "3"
0x63, // "4"
0x5B, // "5"
0x5F, // "6"
0x70, // "7"
0x7F, // "8"
0x7B, // "9"
0x00, // " "
0x67, // "H"
0x0d // "L"
};
char digitup[13] =
{
0xD0, // "0" LCD segments a+b+c+d+e+f
0x00, // "1"
0xE0, // "2" lcd digit d e g f
0xA0, // "3"
0x30, // "4"
0xB0, // "5"
0xF0, // "6"
0x00, // "7"
0xF0, // "8"
0xB0, // "9"
0x00, // " "
0x70, // "H"
0xd0 // "L"
};
char digit9up[11] =
{
0x0D, // "0" LCD segments a+b+c+d+e+f
0x00, // "1"
0x0E, // "2" lcd digit d e g f
0x0A, // "3"
0x03, // "4"
0x0B, // "5"
0x0F, // "6"
0x00, // "7"
0x0F, // "8"
0x0B, // "9"
0x00 // " "
};
char digit6down[11] =
{
0x70, // "0" LCD segments a+b+c+d+e+f
0x60, // "1"
0x30, // "2" lcd digit DP c b a
0x70, // "3"
0x60, // "4"
0x50, // "5"
0x50, // "6"
0x70, // "7"
0x70, // "8"
0x70, // "9"
0x00
};
char digitdown[13] =
{
0x07, // "0" LCD segments a+b+c+d+e+f
0x06, // "1"
0x03, // "2" lcd digit DP c b a
0x07, // "3"
0x06, // "4"
0x05, // "5"
0x05, // "6"
0x07, // "7"
0x07, // "8"
0x07, // "9"
0x00, // " "
0x06, // "H"
0x00 // "L"
};
char ad13data[4];
char ad14data[4];
char ad15data[4];
unsigned char bufnum=0; // mcu receive byte number
unsigned char comlen=0; // mcu receive byte number
unsigned char comoverflag=0; // receive over flag
unsigned char comnum=0; // rs232 communication command number
char buf_str[20]; // mcu receive byte
unsigned char romchgflag=0; // EERAM change flag
unsigned int factor13=0;
unsigned int factor14=0;
unsigned int factor15=0;
char ackrd2402[]={0xed,0xfa,0x06,0x06,0x60,0x05};
void delay(unsigned int i);
void sysinit(void);
void lcdinit(void);
void adcinit(void);
void rs232init(void);
void readad(void);
void readadn(void);
void display(void);
void ledflash1(void);
void ledflash2(void);
void convert35(void);
void getkey(void);
void getpara(void);
void write2402(void);
void read2402(void);
void wr2402(unsigned char address,unsigned char wrdata);
unsigned char rd2402(unsigned char address);
void starti2c(void);
void stopi2c(void);
void writebyte(unsigned char writedata);
unsigned char readbyte(void);
void sendone(void);
void sendtwo(void);
void main(void)
{
sysinit();
lcdinit();
adcinit();
rs232init();
P2SEL &= 0x3F;
P2DIR |= 0x40;
P1IES = BIT4 +BIT5;
P1IE |= BIT4 + BIT5;
_EINT();
while(1)
{
if(romchgflag == 1)
{
romchgflag=0;
getpara();
}
sendone();
readadn();
display();
//ledflash1();
// if(((P1IN&BIT4)!=BIT4)||((P1IN&BIT5)!=BIT5))
// getkey()
if (key==0x01)
{
key = 0;
ledflash1 ();
}
if (key==0x02)
{
key = 0;
ledflash2 ();
}
}
// LPM3; // Enter LPM3
}
void getpara(void)
{
factor13 = 256*rd2402(0) + rd2402(1);
factor13 = 256*rd2402(0) + rd2402(1);
factor14 = 256*rd2402(2) + rd2402(3);
factor15 = 256*rd2402(4) + rd2402(5);
}
void sysinit(void)
{
// volatile unsigned int i; // Use volatile to prevent removal
// by compiler optimization
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
FLL_CTL0 |= XCAP18PF; // Configure load caps,
// if use extendal 20pF cap,will flash when lcd open,cap value can't too large
// for (i = 0; i < 10000; i++); // Delay for 32 kHz crystal to
delay(20000); // stabilize and ADC ref stabilize
delay(20000); // else will flash when lcd open
P3DIR |= 0xFF; // Set P3 to output direction
P3OUT |= 0x09; // Set P3.0=1,P3.3=1
P4DIR |= 0xFF; // Set P4 to output direction
P4OUT |= 0xFC; // Set P4.2,P4.3,P4.4,P4.5,P4.6,P4.7=1
//P1DIR = 0x01;
//Set p1.0 to output and p1.1-p1.7 to input direction
// P1IE |= 0x00; //The p1.0-p1.7 interrupt is disable
// P1OUT &= 0xFE; //Set p1.0=0
romchgflag=1;
}
void lcdinit(void)
{
unsigned int i;
LCDCTL = LCDSG0_5 + LCD4MUX + LCDON; // 4-Mux LCD, segments S0-S31
delay(100);
BTCTL = BT_fLCD_DIV64; // LCD clock freq is ACLK/256
P5SEL = 0xFC; // Select P5.2-7 as Com and Rxx
for (i=0; i<15; i++)
{
LCD[i] = 0;
delay(1000);
}
}
void adcinit(void)
{
ADC12CTL0 = SHT0_15 + MSC + REF2_5V + REFON + ADC12ON;
// Set sampling time 1024ADC12CLK, turn on ADC12,REF2.5V
ADC12MCTL3 = SREF_1 + INCH_3; //Set reference V+=Vref+,V-=AVss,Channel A3
ADC12MCTL4 = SREF_1 + INCH_4; //Set reference V+=Vref+,V-=AVss,Channel A4
ADC12MCTL5 = SREF_1 + INCH_5 +EOS; //Set reference V+=Vref+,V-=AVss,Channel A5,END
ADC12CTL1 = CSTARTADD_3 + SHP + CONSEQ_1; // Start address,Use sampling timer,Single sequence
ADC12IE = 0x20; // Enable interrupt
ADC12CTL0 |= ENC; // Conversion enabled
P6SEL |= 0x38; // P6.3,P6.4,P6.5, ADC option select
}
void rs232init(void)
{
P2SEL|=0x30; // P2.4,5 = USART0 TXD/RXD
ME1|=UTXE0+URXE0; // Enable USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL1; // UCLK = SMCLK
UBR00 = 0x6D; // 1MHz 9600 1048576Hz/9600 = ~109.23 (06Dh|03h)
UBR10 = 0x00; // 1MHz 9600
UMCTL0 =0x03; // modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // Enable USART0 RX interrupt
P2DIR |= 0x10; // P2.4 output direction
// bufnum=0;
}
// RS232 interrupt service routine
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
char i,tempsave,yihou=0;
ADC12IE = 0x00; // close ADC interrupt
while (IFG1 & URXIFG0);
tempsave=RXBUF0;
if(bufnum==0)
{
if(tempsave==0xef) buf_str[0]=0xef;
else bufnum=0xff;
}
if(bufnum==1)
{
if(tempsave==0xef) buf_str[1]=0xef;
else bufnum=0xff;
}
if(bufnum==2) buf_str[2]=tempsave;
if(bufnum==3)
{
if(tempsave==buf_str[2])
{
buf_str[3]=tempsave;
comlen=buf_str[3]+2;
}
else bufnum=0xff;
}
if(bufnum>3)
{
if(bufnum<comlen)
{
buf_str[bufnum]=tempsave;
}
else if(bufnum==comlen)
{
buf_str[bufnum]=tempsave;
bufnum=0xff;
comoverflag=1;
}
}
if((comoverflag==1)&&(buf_str[4]==0x60))
{
for(i=0;i<comlen;i++)
{
yihou+=buf_str[i];
}
if(buf_str[comlen]==yihou)
{
comnum=buf_str[5];
}
else
{
comnum=0xff;
comlen=0xff;
bufnum=0xff;
}
switch(comnum)
{
case 1:
sendone();
break;
case 2:
sendtwo();
break;
case 3:
write2402();
break;
case 4:
read2402();
break;
default:
break;
}
buf_str[0]=0x00;
buf_str[0]=0x00;
}
comoverflag=0;
// while (!(IFG1 & UTXIFG0));
// TXBUF0 = buf_str[bufnum];
bufnum++;
ADC12IE = 0x20; // open ADC interrupt
// while (!(IFG1 & UTXIFG0)); // while ((IFG1 & URXIFG0)); USART0 TX buffer ready?
}
void sendone(void)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = /*resultsum[0];*/ 0x01; // RXBUF0 to TXBUF0
while (!(IFG1 & UTXIFG0));
TXBUF0 = /*resultsum[1];*/0x02; // RXBUF0 to TXBUF0
while (!(IFG1 & UTXIFG0));
TXBUF0 = /*resultsum[2];*/0x03; // RXBUF0 to TXBUF0
}
void sendtwo(void)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = 0x0a; // RXBUF0 to TXBUF0
while (!(IFG1 & UTXIFG0));
TXBUF0 = 0x0b; // RXBUF0 to TXBUF0
while (!(IFG1 & UTXIFG0));
TXBUF0 = 0x0c; // RXBUF0 to TXBUF0
}
void write2402(void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -