I have been trying to get my code to run two functions at different intervals. I want my UART update to happen at about every 200ms and my SPI commands to about every 5ms. CCR0 works for me, but CCR1 does not. Both functions execute but CCR1 seems to execute as quickly as possible while CCR0 executes about every 200ms. Can anyone see what I am missing here?
#include
<msp430.h>
#define
MYTRUE 1
#define
MYFALSE 0
#define
numcycles 10
#define
update_delay 2620//1310 //0
#define
page_write 3000 //5
#define
recharge_cycles 3000000
extern void CSL_init(void);
void adc2asciidec(unsigned long anum);
void
adc2asciihex(unsigned long anum);
unsigned
char MST_Data, SLV_Data;
volatile
unsigned int i;
float
curr_V;
unsigned
long device_id;
unsigned
long charge_cnt;
unsigned
long bytes_wr;
int
first_run;
unsigned
char digit[16];
unsigned
long lnum;
char
lnum2;
void
main(int argc, char *argv[])
{
CSL_init();
//confirmation copy
WDTCTL = WDTPW + WDTHOLD;
UCA0CTL1 |= UCSWRST;
UCB0CTL1 |= UCSWRST;
ADC10CTL0 &= ~ENC;
UCB0CTL0 =
/*UCCKPL +*/ UCCKPH + UCMSB + UCMST + UCMODE_0; //+ UCSYNC;
ADC10CTL0 = ADC10IE + ADC10ON +
/*REFON +*/ ADC10SHT_3 + SREF_0;
UCA0CTL1 = UCSSEL_2 + UCSWRST;
UCB0CTL1 = UCSSEL_2 + UCSWRST;
ADC10CTL1 = CONSEQ_0 + ADC10SSEL_0 + ADC10DIV_3 + SHS_0 + INCH_0;
UCA0MCTL = UCBRF_0 + UCBRS_1;
UCA0BR0 = 104;
//64;
UCA0BR1 = 0;
//3;
UCB0BR0 |= 0x1;
UCB0BR1 = 0;
ADC10CTL0 |= ENC;
UCA0CTL1 &= ~UCSWRST;
UCB0CTL1 &= ~UCSWRST;
P2OUT = BIT0;
P1SEL = BIT1 + BIT2 + BIT5 + BIT6 + BIT7;
P1SEL2 = BIT1 + BIT2 + BIT5 + BIT6 + BIT7;
P1DIR = BIT3 + BIT4;
P2DIR = BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7;
P1IES = 0;
P1IFG = 0;
P2IES = 0;
P2IFG = 0;
IFG2 &= ~(UCA0RXIFG + UCB0RXIFG);
IE2 |= UCA0RXIE;
IE2 |= UCB0TXIE;
IE2 |= UCB0RXIE;
TA0CCTL0 = CCIE;
TA0CCTL1 = CCIE;
TA0CCR0 = update_delay;
TA0CCR1 = page_write;
TA0CTL = TASSEL_1 + MC_1 + TAIE;
// ACLCK, 1/8 DIVIDER, upmode to TCCR0 value
charge_cnt = 0;
bytes_wr = 0;
lnum2 = 0x01;
first_run = MYFALSE;
P2OUT |= BIT1;
// Vdd
__delay_cycles(recharge_cycles);
// Wait for slave to initialize
P2OUT &= ~BIT1;
// Vdd
for(;;){
__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupt
}
}
void
USCIA0RX_ISR(void)
{
int x = 0;
int y = 0;
lnum2 = 0x01;
ADC10CTL0 &= ~ENC;
while (ADC10CTL1 & BUSY); // Wait if ADC10 core is active
ADC10CTL0 |= ENC + ADC10SC;
lnum = ADC10MEM;
if (lnum < 559)//512
{
P2OUT |= BIT1;
__delay_cycles(recharge_cycles);
P2OUT &= ~BIT1;
__delay_cycles(200);
charge_cnt++;
}
P2OUT &= ~BIT0;
// /CE low
__delay_cycles(numcycles);
// Add time between transmissions to
UCB0TXBUF = 0x06;
// WREN
__delay_cycles(numcycles);
// Add time between transmissions to
P2OUT |= BIT0;
// /CE high
P2OUT &= ~BIT0;
// /CE low
__delay_cycles(numcycles);
// Add time between transmissions to
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0x02;
// write op code
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0x01;
// write MSB address
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = MST_Data;
// write LSB address
for (x = 0; x < 32; x++)
{
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0xAA;
// write 1byte
}
__delay_cycles(numcycles);
// Add time between transmissions to
P2OUT |= BIT0;
// /CE high
while
((lnum2 % 2) > 0)
{
P2OUT &= ~BIT0;
// /CE low
__delay_cycles(numcycles);
// Add time between transmissions to
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0x05;
// read op code
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0xFF;
// dummy for clock
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0xFF;
// dummy for clock
while (!(IFG2 & UCB0RXIFG));
lnum2 = UCB0RXBUF;
// write 1byte
__delay_cycles(numcycles);
// Add time between transmissions to
P2OUT |= BIT0;
// /CE high
y++;
}
if
(y > 1)
{
device_id = 1;
}
else
{
device_id = 0;
}
lnum2 = 0x01;
bytes_wr = bytes_wr + 32;
MST_Data++;
__delay_cycles(numcycles);
// Add time between transmissions to
TA0CCR1 = page_write;
}
void
TIMER_ISR(void)
{
adc2asciihex(device_id);
adc2asciihex(lnum);
adc2asciihex(charge_cnt);
adc2asciihex(bytes_wr);
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =
'\r'; // Send next value
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =
'\n'; // Send next value
TA0CCR0 = update_delay;
}
void
adc2asciidec(unsigned long anum)
{
int i=0;
while(anum >= 10)
{
digit[i] = anum % 0x0a;
anum = anum / 0x0a;
i++;
}
digit[i] = anum;
while(i>=0)
{
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = digit[i] + 48;
//uart_data[x];//ADC10MEM; // Send next value
digit[i] = 0;
i--;
}
//delay_ms(500);
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =
' '; // Send next value
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =
' '; // Send next value
}
void
adc2asciihex(unsigned long anum)
{
int i=0;
while(anum >= 0x10)
{
digit[i] = anum % 0x10;
anum = anum / 0x10;
i++;
}
digit[i] = anum;
while(i>=0)
{
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
if (digit[i] < 10)
{
UCA0TXBUF = digit[i] + 48;
//uart_data[x];//ADC10MEM; // Send next value
}
else
{
UCA0TXBUF = digit[i] - 10 + 65;
//uart_data[x];//ADC10MEM; // Send next value
}
digit[i] = 0;
i--;
}
//delay_ms(500);
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =
' '; // Send next value
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =
' '; // Send next value
}
#pragma vector = TIMER0_A1_VECTOR
__interrupt
void TIMER_A1(void)
{
USCIA0RX_ISR();
}
#pragma
vector = TIMER0_A0_VECTOR
__interrupt
void TIMER_A0(void)
{
TIMER_ISR();
}