📄 msp430demo.c
字号:
{
if(buf_str[7]==1) wr2402(buf_str[6],buf_str[9]);
if(buf_str[7]==2)
{
delay(500);
wr2402(buf_str[6],buf_str[8]);
delay(500); // must delay enough time
wr2402(buf_str[6]+1,buf_str[9]);
// delay(500);
// wr2402(buf_str[6],buf_str[8]);
// delay(500);
// wr2402(buf_str[6]+1,buf_str[9]);
}
romchgflag=1;
while (!(IFG1 & UTXIFG0));
TXBUF0 = 0xaa; // RXBUF0 to TXBUF0
while (!(IFG1 & UTXIFG0));
TXBUF0 = 0xbb; // RXBUF0 to TXBUF0
while (!(IFG1 & UTXIFG0));
TXBUF0 = 0xcc; // RXBUF0 to TXBUF0
}
void read2402(void)
{
unsigned char i,data1=0,data2=0,datayihou;
if(buf_str[7]==1) data1=rd2402(buf_str[6]);
if(buf_str[7]==2)
{
delay(500);
data1=rd2402(buf_str[6]);
delay(500); // must delay enough time
data2=rd2402(buf_str[6]+1);
// delay(500);
// data1=rd2402(buf_str[6]);
// delay(500);
// data2=rd2402(buf_str[6]+1);
}
datayihou=data1+data2+0x58;
for(i=0;i<6;i++)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = ackrd2402[i]; // RXBUF0 to TXBUF0
}
while (!(IFG1 & UTXIFG0));
TXBUF0 = data1;
while (!(IFG1 & UTXIFG0));
TXBUF0 = data2;
while (!(IFG1 & UTXIFG0));
TXBUF0 = datayihou;
}
/********************** general I2C program **********************/
void starti2c(void)
{
SCL_0;
delay(20);
SDA_1;
delay(20);
SCL_1;
delay(20);
SDA_0;
delay(20);
}
void stopi2c(void)
{
SCL_0;
delay(20);
SDA_0;
delay(20);
SCL_1;
delay(20);
SDA_1;
delay(20);
}
void writebyte(unsigned char writedata)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL_0;
delay(20);
DIR_OUT;
delay(20);
if(((writedata>>7)&0x01) == 0x01)
{
SDA_1;
}
else
{
SDA_0;
}
delay(20);
SCL_1;
writedata=writedata<<1;
delay(20);
}
SCL_0;
delay(20);
SCL_1;
delay(20);
}
unsigned char readbyte(void)
{
unsigned char i,tempbit=0,tempdata=0;
SCL_0;
delay(20);
DIR_IN;
for(i=0;i<8;i++)
{
delay(20);
SCL_1;
delay(20);
if(((P2IN >> 7) & 0x01) == 0x01) // if SDA input equal 1
{
tempbit=1;
}
else
{
tempbit=0;
}
tempdata=((tempdata<<1) | tempbit);
SCL_0;
}
DIR_OUT;
delay(20);
SDA_0;
delay(20);
SCL_1;
delay(20);
SDA_1;
delay(20);
return(tempdata);
}
unsigned char rd2402(unsigned char address)
{
unsigned char tempchar;
starti2c();
writebyte(0xa0);
writebyte(address);
starti2c();
writebyte(0xa1);
tempchar=readbyte();
return(tempchar);
}
void wr2402(unsigned char address,unsigned char writedata)
{
starti2c();
writebyte(0xa0);
writebyte(address);
writebyte(writedata);
stopi2c();
}
/********************** general I2C program **********************/
void delay(unsigned int i)
{
while(i--);
}
void readadn(void) // average for 64
{
unsigned char i;
for(i=0;i<64;i++)
{
readad();
delay(100);
resultsum[0] += results[0];
resultsum[1] += results[1];
resultsum[2] += results[2];
}
resultsum[0] = resultsum[0]/64.0;
resultsum[1] = resultsum[1]/64.0;
resultsum[2] = resultsum[2]/64.0;
resultsum[0] = (resultsum[0]*2500.0)/4096;
resultsum[1] = (resultsum[1]*2500.0)/4096;
resultsum[2] = (resultsum[2]*2500.0)/4096;
}
void readad(void)
{
ADC12CTL0 |= ADC12SC; // Sampling open
_BIS_SR(CPUOFF + GIE); // LPM0, Enter low power mode,ADC12_ISR will force exit
// GIE , General interrupt enable
delay(100);
}
// ADC12 interrupt service routine
#pragma vector=ADC_VECTOR
__interrupt void ADC12_ISR (void)
{
results[0] = ADC12MEM3; // Move A3 results, IFG is cleared
results[1] = ADC12MEM4; // Move A4 results, IFG is cleared
results[2] = ADC12MEM5; // Move A5 results, IFG is cleared
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR),Exit low power mode
_NOP(); // SET BREAKPOINT HERE
}
void display(void) // onlg display the next three digit of the sample voltage
{
unsigned int tempint; // range = 0 to 65535
tempint = resultsum[0] + factor13;
// ad13data[0] = tempint/1000;
// if(ad13data[0] == 0) ad13data[0]=10;
ad13data[1] = tempint%1000/100;
if(ad13data[1] == 0) ad13data[1]=10;
ad13data[2] = tempint%1000%100/10;
if((ad13data[2] == 0)&&(ad13data[1] == 10)) ad13data[2]=10;
ad13data[3] = tempint%1000%100%10;
LCD[12]=digitud[ad13data[1]]; // digit 1
LCD[13]=digitud[ad13data[2]]; // digit 2
LCD[14]=digitud[ad13data[3]]|0x80; // digit 3 and 1310nm
tempint = resultsum[1] + factor14;
// ad14data[0] = tempint/1000;
// if(ad14data[0] == 0) ad14data[0]=10;
ad14data[1] = tempint%1000/100;
if(ad14data[1] == 0) ad14data[1]=10;
ad14data[2] = tempint%1000%100/10;
if((ad14data[2] == 0)&&(ad14data[1] == 10)) ad14data[2]=10;
ad14data[3] = tempint%1000%100%10;
// digit 4 ****************************
templcd=LCD[9];
templcd&=0x0F;
templcd|=digitup[ad14data[1]];
LCD[9]=templcd;
templcd=LCD[10];
templcd&=0xF8;
templcd|=digitdown[ad14data[1]];
LCD[10]=templcd;
// digit 4 ****************************
// digit 5 ****************************
templcd=LCD[10];
templcd&=0x0F;
templcd|=digitup[ad14data[2]];
LCD[10]=templcd;
templcd=LCD[11];
templcd&=0xF0;
templcd|=digitdown[ad14data[2]];
LCD[11]=templcd;
// digit 5 ****************************
// digit 6 ****************************
templcd=LCD[11];
templcd&=0x0F;
templcd|=digitup[ad14data[3]];
LCD[11]=templcd;
templcd=LCD[5];
templcd&=0x0F;
templcd|=0x80;
templcd|=digit6down[ad14data[3]];
LCD[5]=templcd;
// digit 6 ****************************
tempint = resultsum[2] + factor15;
// ad15data[0] = tempint/1000;
// if(ad15data[0] == 0) ad15data[0]=10;
ad15data[1] = tempint%1000/100;
if(ad15data[1] == 0) ad15data[1]=10;
ad15data[2] = tempint%1000%100/10;
if((ad15data[2] == 0)&&(ad15data[1] == 10)) ad15data[2]=10;
ad15data[3] = tempint%1000%100%10;
// digit 7 ****************************
LCD[7]&=0x80;
LCD[7]|=digitud[ad15data[1]];
// digit 7 ****************************
LCD[8]=digitud[ad15data[2]]; // digit 8
// digit 9 ****************************
templcd=LCD[9];
templcd&=0xF0;
templcd|=digit9up[ad15data[3]];
LCD[9]=templcd;
templcd=LCD[5];
templcd&=0xF0;
templcd|=0x08;
templcd|=digitdown[ad15data[3]];
LCD[5]=templcd;
// digit 9 ****************************
LCD[6]|=0xEE; // three dBm
}
void getkey(void)
{
if((P1IN&BIT4)!=BIT4)
{
delay(1000);
if((P1IN&BIT4)!=BIT4)
{
convert35();
}
while((P1IN&BIT4)!=BIT4);
}
else if((P1IN&BIT5)!=BIT5)
{
delay(1000);
if((P1IN&BIT5)!=BIT5)
{
ledflash2();
// return (0);
}
while((P1IN&BIT5)!=BIT5);
}
// return (1) ;
}
void ledflash1(void)
{
P3OUT ^= BIT3;//0x08; //Toggle P3.3 using exclusive-OR
delay(10000);
}
void ledflash2(void)
{
P3OUT ^= BIT0;//0x01; //Toggle P3.3 using exclusive-OR
delay(10);
}
void convert35(void)
{
P4OUT ^= 0xFC;
delay(10);
}
#pragma vector = PORT1_VECTOR
__interrupt void Port1 (void)
{
if (P1IFG&BIT4)
{
P1IFG &=~BIT4;
delay(1000);
if((P1IN&BIT4)!=BIT4)
{
key = key1;
}
}
if (P1IFG&BIT5)
{
P1IFG &=~BIT5;
delay(1000);
if((P1IN&BIT5)!=BIT5)
{
key = key2;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -