1371 views|0 replies
- Last login
- 2023-11-15
- Online Time
- 196 hours
- Prestige
- 1018 points
- Points
- 641 points
|
msp430 MCU ADC12 sampling voltage to 1602 display program
[Copy link]
#include
#include "Config.h" //Development board configuration header file, mainly configure IO port information #include "1602.c" static uchar Flag=0; //Flag variable uint TEMP=0; //ADC value temporary storage variable uint temp,A1,A2,A3,A4,A5,A6,A7; //Defined variables, display data processing//*************************************************************************** // Display the collected ADC value//*************************************************************************** void LCD_DisplayADC() { LCD_write_char(0x0b,0,0x30+A1); LCD_write_char(0x0c,0,0x30+A2); LCD_write_char(0x0d,0,0x30+A3); LCD_write_char(0x0e,0,0X30+A4); } void LCD_DisplayVoltage() { LCD_write_char(0x0b,1,0x30+A5); LCD_write_char(0x0c,1,'.'); LCD_write_char(0x0d,1,0x30+A6); LCD_write_char(0x0e,1,0X30+A7); LCD_write_char(0x0f,1,'V'); } //**************************************************************************** // Digital quantity display processing function//**************************************************************************** void Data_do(uint temp_d) { uint temp_1,temp_2; A1=temp_d/1000; // Separate thousands, hundreds, tens, and units temp_1=temp_d%1000; A2=temp_1/100; temp_2=temp_1%100; A3=temp_2/10; A4=temp_2%10; } //************************************************************************ // Voltage data display processing function//************************************************************************ void Voltage_do(uint temp_d) { uint temp_1,temp_2; ulong temp_3; temp_3=(ulong)(temp_d)*250; //Conversion formula, ADC digital quantity is converted to voltage size, pay attention to the data type temp_d=temp_3/4095; //12-bit accuracy, divide by 4095 A5=temp_d/100; //Separate hundreds, tens, and ones temp_1=temp_d%100; A6=temp_1/10; temp_2=temp_1%10; A7=temp_2; } //***************************************************************************** // ADC initialization procedure, used to configure ADC related registers //************************************************************************ void ADC_Init() { P6SEL|=0x01; //Select ADC channel ADC12CTL0|= ADC12ON + SHT0_2 + REF2_5V + REFON; //ADC power control on, 16 CLK, internal reference 2.5V ADC12CTL1|= ADC12SSEL1 + ADC12SSEL0; //SMCLK as clock source ADC12MCTL0= SREF0 + INCH_0; //Refer to control bit and channel selection, select channel 0 here ADC12IE|= 0x01; //Interrupt enable ADC12CTL0|= ENC; //Enable converter} //**************************************************************************** // ADC interrupt service routine//**************************************************************************** #pragma vector=ADC_VECTOR __interrupt void ADC12ISR(void) { uchar j; while((ADC12CTL1&0x01)==1); //If ADC is busy, wait, otherwise read ADC conversion value Flag = 1 ; TEMP = ADC12MEM0; //Read ADC conversion value Data_do(TEMP); //Process ADC value for display, digital quantity Voltage_do(TEMP); //Process ADC value for display, analog voltage value for(j=0;j<15;j++) { LCD_DisplayADC(); //Display ADC data, ADC digital quantity LCD_DisplayVoltage(); //Display ADC voltage value } } //*************************************************************************** // Main program//*************************************************************************** void main(void) { WDT_Init(); //Watchdog initialization Clock_Init(); //Clock initialization Port_Init(); //Port initialization, used to control IO port input or output ADC_Init(); //Initialize ADC configuration delay_ms(100); //Delay 100ms LCD_init(); //LCD parameter initialization settings LCD_clear(); //Clear screen LCD_Desk(); _EINT(); //Enable interrupt Flag=1; //Set the flag to 1 first while(1) { while(Flag==1) { ADC12CTL0 |= ADC12SC; //Start conversion ADC12CTL0 &= ~ADC12SC; //Clear Flag=0; //Clear flag } } }
|
|