dimanche 7 juin 2020

Interpret command from PC to control microcontroller LED

I am not sure how to code a function that will interpret the commands received from a PC. The syntax of the commands is as follows: leds -1+4~3+2

the first 5 characters of the command must always be 'leds '. a digit means the corresponding LED number (1-4). A leading plus (+) means that this LED is turned on, a minus (–) that it is turned off, and a tilde (~) means that the LED is toggled once.

see below the current code. it complies without erros however still doesnt work and im not sure which part is wrong...

void main(void){
unsigned short int i = 0;
unsigned short int j = 1;
unsigned short int led = 0;


 while((UART0_FR_R & (1<<4)));
 input[i] = UART0_DR_R;

 //this next line i am tring to check that the first 5 characters equal 'leds ' but i think what ive 
 //written is incorrect and i dont know what to replace it with
 while(input[0] == 'l' && input[0] == 'e' && input[0] == 'd' && input[0] == 's' && input[0] == ' ')
{
         if(input[4+i] == '+'){
            input[4+j] == led;
             if(led == 1){
                 GPIO_PORTN_DATA_R = 0x02;
             }
             else if(led == 2){
                 GPIO_PORTN_DATA_R = 0x01;
             }
             else if(led == 3){
                 GPIO_PORTF_AHB_DATA_R = 0x10;
             }
             else if(led == 4){
                 GPIO_PORTF_AHB_DATA_R = 0x01;
             }
         }
         else if(input[4+i] == '-'){
             input[4+j] == led;
              if(led == 1 || led == 2){
                  GPIO_PORTN_DATA_R = 0x00;
              }
              else if(led == 3 || led == 4){
                  GPIO_PORTF_AHB_DATA_R = 0x00;
              }
         }
         else if(input[4+i] == '~'){
             input[4+j] == led;
               if(led == 1){
                   GPIO_PORTN_DATA_R &= ~0x02;
               }
               else if(led == 2){
                   GPIO_PORTN_DATA_R &= ~0x01;
               }
               else if(led == 3){
                   GPIO_PORTF_AHB_DATA_R &= ~0x10;
               }
               else if(led == 4){
                   GPIO_PORTF_AHB_DATA_R &= ~0x01;
               }
         }

         i++;
         j++;
 }

Aucun commentaire:

Enregistrer un commentaire