I am using TivaC TM4C123 MCU. I am trying to send data serially through one pin and a synchronized clock through another pin. Here is an illustration:
I used Timer0 module for the serial data and Timer1 for the clock. The frequency of the clock should be twice the data. Here is the important part of the code:
void DATA(long number, int iteration){ //number: data to be send serially long x=0; // iteration: number of bits to be send for(int y=0; y<iteration ; y++){ while((TIMER0->RIS & 0x00000001) != 1){} //wait until Timer0 times out x= number & (0x1<<y); //Get each bit individually if(x==(0X1<<y)) //If bit is 1 GPIOF->DATA |= (1<<1); //Make PF1 High else if(x==0X0) //If bit is 0 GPIOF->DATA &= 0XFD; //Make PF1 Low TIMER0->ICR |= (1<<0); //Reset Timer0 flag while((TIMER1->RIS & 0x00000001) != 1){} //wait until Timer1 times out GPIOF->DATA ^= (1<<2); //Toggle clock TIMER1->ICR |= (1<<0); //Reset Timer1 flag } } int main(){ GPIO_INIT(); // Initiate GPIO TIMER_INIT(); // Initiate Timers with Timer0= 1/2 Timer1 while (1){ DATA(0XAB01,16); // 0xAB01 16 bit data need to be send serially } }
There is a problem in this code, which is both of the data and the clock have the same frequency, which means that Timer0 or Timer1 are disabled.
To make my problem more clear and easy to understand. I wrote two simple codes with a graph for each of them. Here is the first code: I am using While loop
int main() { GPIO_INIT(); TIMER_INIT(); //Timer0= 1/2 Timer1 while (1){ while((TIMER0->RIS & 0x00000001) != 1){} //Wait for TIMER0 to time out GPIOF->DATA ^= (1<<1); //Toggle PF1 TIMER0->ICR |= (1<<0); //Reset TIMER0 flag while((TIMER1->RIS & 0x00000001) != 1){} //Wait for TIMER1 to time out GPIOF->DATA ^= (1<<2); // Toggle PF2 TIMER1->ICR |= (1<<0); //Reset TIMER1 flag } }
Here is the output I got:
Here is the 2nd code: I am using If condition
int main(){ GPIO_INIT(); TIMER_INIT(); //Timer0= 1/2 Timer1 while (1){ if((TIMER0->RIS & 0x00000001) == 1){ //If TIMER0 out timed out GPIOF->DATA ^= (1<<1); //Toggle PF1 TIMER0->ICR |= (1<<0); //Reset TIMER0 flag } if((TIMER1->RIS & 0x00000001) == 1){ //If TIMER1 timed out GPIOF->DATA ^= (1<<2); //Toggle PF2 TIMER1->ICR |= (1<<0); //Reset TIMER1 flag } } }
Here is the output:
Conclusion: When I used the if condition the two timers are working normally, but when I used While loop only one timer is working and the other timer is just using the same counts of the other.