good am everyone.. i've started using msp430g2553 to be used to interface lm35 and will give a pwm output depending on the PID results. here's my code:
#include <msp430g2553.h>
float act_temp;
int des_temp = 5;
float error = 0;
float error_sum = 0;
float error_prev = 0;
float error_diff = 0;
float kp = 10000;
float ki = 50;
float kd = 1;
float dt = 1;
float pid;
int pwm;
int k;
char data[5];
void TX(char *tx_message);
unsigned int measure;
unsigned int measure_1;
unsigned int measure_2;
void main(void){
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2;
P1SEL2 = BIT1 + BIT2;
UCA0CTL1 |= UCSSEL_2;
UCA0BR0 = 104;
UCA0BR1 = 0;
UCA0MCTL = UCBRS0;
UCA0CTL1 &= ~UCSWRST;
ADC10CTL0 |= SREF_0|ADC10ON|ADC10SHT_2;//ADC setup
ADC10CTL1 |= INCH_4|ADC10SSEL_1|CONSEQ_1;
ADC10AE0 |= BIT0;
ADC10CTL0 |= ENC|ADC10SC;
TACTL = TASSEL_1|ID_3|MC_1|TAIE;
TA0CCTL1 = OUTMOD_7;
TACCR0 = 50000;
for(;;)
{
ADC10CTL0 |= ENC + ADC10SC;
int raw_temp = ADC10MEM;
float temp = (ADC10MEM*350)/1024;
// des_temp = UCA0RXBUF;
act_temp = temp;
error = (act_temp - des_temp);
error_sum = error_sum + error;
error_diff = (error - error_prev)/dt;
pid = (kp*error) + (ki*error_sum) + (kd*error_diff);
pwm = (int)pid;
error_prev = error;
P1SEL |= BIT6;
P1OUT |= BIT6;
TACCR1 = pwm;
if (pwm < 0){pwm = 0;}
if (pwm > TACCR0){pwm = TACCR0;}
//sending data to uart
int measure1000; //declare measure1000 as interger
int measure100; //declare measure100 as interger
int measure10; //declare measure10 as interger
int measure1; //declare measure1 as interger
measure1000 = ((raw_temp)/1000)%10; // measure10 is the 10's place digit
measure100 = ((raw_temp)/100)%10; // measure100 is the 100's place digit
measure10 = ((raw_temp)/10)%10; // measure10 is the 10's place digit
measure1 = (raw_temp)%10; // measure1 is the 1's place digit
data[0] = (char)(((int)'0')+measure1000); //measure10 - 2nd char string
data[1] = (char)(((int)'0')+measure100); //measure1 - 3rd char string
data[2] = (char)(((int)'0')+measure10); //measure10 - 2nd char string
data[3] = (char)(((int)'0')+measure1); //measure1 - 3rd char string
data[4]='\0'; //end of character string
//send data
TX(data);
TX("\r");
__delay_cycles(1000000);
}
}
void TX(char *tx_message)
{
unsigned int i=0; //Define end of string loop int
char *message; // message variable
unsigned int message_num; // define ascii int version variable
message = tx_message; // move tx_message into message
while(1)
{
if(message[i]==0) // If end of input string is reached, break loop.
{
break;
}
message_num = (int)message[i]; //Cast string char into a int variable
UCA0TXBUF = message_num; // write INT to TX buffer
i++; // increase string index
__delay_cycles(250000); //transmission delay
if(i>50) //prevent infinite transmit
{
P1OUT |= BIT6; //P1OUT LED will indicate error
break; //Break the transmit
}
} // End TX Main While Loop
} // End TX Function
// 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)
}
P1.0 is the input from lm35, P1.6 is the pwm output...
but i've the raw_temp is not changing at all, even if the temperature sensor is placed to a different environment. could you give me some advice and tips to improve my code.. thank you very much..