YouTube channel: https://youtu.be/2g9ohxlH3s0
unsigned int i; //Counter
void int_char();
int j=0;
char str[4];
int temp = 0;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
/*Port 2 disabled*/
P2DIR = 0xFF; //All P2.x output
P2OUT &= 0x00; //reset
/* Use Calibration values for 1MHz Clock DCO*/
DCOCTL = 0;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
/* Configure Pin Muxing P1.1 RXD and P1.2 TXD */
P1SEL = BIT1 | BIT2 ;
P1SEL2 = BIT1 | BIT2;
/* Place UCA0 in Reset to be configured */
UCA0CTL1 = UCSWRST;
/* Baud Rate configuration */
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
/* Take UCA0 out of reset */
UCA0CTL1 &= ~UCSWRST;
/*Configure ADC10*/
ADC10CTL0 = SREF_1 + REFON + ADC10ON + ADC10SHT_3 + ADC10IE;
ADC10CTL1 = INCH_10 + ADC10DIV_3;
while(1)
{
j=0;
IE2 |= UCA0RXIE; //Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
temp = ADC10MEM; //ADC10 conversion value stored in temp
temp = ((temp * 27069L - 18169625L)>>16); //value converted to degree celsius
j=temp;
int_char(); //convert int to char string
UC0IE |= UCA0TXIE; //transmit the value
//i=0; //reset the counter
}
}
/* RX ISR : Start conversion on demand, i.e. whenever t is received*/
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
if (UCA0RXBUF == 't')
{
ADC10CTL0 |= ENC + ADC10SC; //Start Conversion
}
}
/* TX ISR : Transmit the temperature value*/
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
UCA0TXBUF = str[i++]; // TX next character
if (i==sizeof str -1) //Tx over?
{
UC0IE &= ~UCA0TXIE; //Disable Tx interrupt
i=0;
}
}
/*ADC10 ISR: switch ON the CPU*/
#pragma vector = ADC10_VECTOR
__interrupt void adc_interrupt(void)
{
__bic_SR_register_on_exit( CPUOFF); // Return to active mode
}
/*Convert the int value to char string*/
void int_char()
{
int s=0;
s=j%10;
str[2]=(char)(s+48);
j=j/10;
s=j%10;
str[1]=(char)(s+48);
j=j/10;
s=j%10;
str[0]=(char)(s+48);
str[3]='\0';
}
Do Subscribe to it for more projects related to embedded systems.
Software Code used on Code Composer Studio:
#include <msp430.h>
unsigned int i; //Counter
void int_char();
int j=0;
char str[4];
int temp = 0;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
/*Port 2 disabled*/
P2DIR = 0xFF; //All P2.x output
P2OUT &= 0x00; //reset
/* Use Calibration values for 1MHz Clock DCO*/
DCOCTL = 0;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
/* Configure Pin Muxing P1.1 RXD and P1.2 TXD */
P1SEL = BIT1 | BIT2 ;
P1SEL2 = BIT1 | BIT2;
/* Place UCA0 in Reset to be configured */
UCA0CTL1 = UCSWRST;
/* Baud Rate configuration */
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
/* Take UCA0 out of reset */
UCA0CTL1 &= ~UCSWRST;
/*Configure ADC10*/
ADC10CTL0 = SREF_1 + REFON + ADC10ON + ADC10SHT_3 + ADC10IE;
ADC10CTL1 = INCH_10 + ADC10DIV_3;
while(1)
{
j=0;
IE2 |= UCA0RXIE; //Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
temp = ADC10MEM; //ADC10 conversion value stored in temp
temp = ((temp * 27069L - 18169625L)>>16); //value converted to degree celsius
j=temp;
int_char(); //convert int to char string
UC0IE |= UCA0TXIE; //transmit the value
//i=0; //reset the counter
}
}
/* RX ISR : Start conversion on demand, i.e. whenever t is received*/
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
if (UCA0RXBUF == 't')
{
ADC10CTL0 |= ENC + ADC10SC; //Start Conversion
}
}
/* TX ISR : Transmit the temperature value*/
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
UCA0TXBUF = str[i++]; // TX next character
if (i==sizeof str -1) //Tx over?
{
UC0IE &= ~UCA0TXIE; //Disable Tx interrupt
i=0;
}
}
/*ADC10 ISR: switch ON the CPU*/
#pragma vector = ADC10_VECTOR
__interrupt void adc_interrupt(void)
{
__bic_SR_register_on_exit(
}
/*Convert the int value to char string*/
void int_char()
{
int s=0;
s=j%10;
str[2]=(char)(s+48);
j=j/10;
s=j%10;
str[1]=(char)(s+48);
j=j/10;
s=j%10;
str[0]=(char)(s+48);
str[3]='\0';
}
No comments:
Post a Comment