I am experimenting with USART capabilities of my ATMega8, and am trying to write code that that will let my send commands to the ATMega8 via a serial connection with my PC. The commands will cause a servo wired to the circuit to rotate a given direction depending on the command received via the serial connection. For example, I want the code to work as follows:
- if user enters "1" in terminal -> servo rotates to full counter-clockwise
if user enters "2" in terminal -> servo rotates to center
if user enters "3" in terminal -> servo rotates full clockwise
if((ServoCommand == 1) || (ServoCommand == 2) || (ServoCommand == 3)) { switch(ServoCommand) { case 1: OCR1A = 0x258; //rotate to -90 degress (left) break; case 2: OCR1A = 0x5DC; //rotate to 0 degress (center) break; case 3: OCR1A = 0x960; //rotate to 90 degress (right) break; } //end swtich } //end if
Say, the user enters and sends "1" via hyper-terminal, as I understand things (I think) the UDR register will receive the following 8 bits -> 00110001. So, I need to write code that will convert or interpret those 8 bits as a a value of 1 and store that value in my variable "ServoCommand."
Pseudo-code akin to the following:
ReadByte = UDR if ReadByte = 0b00110001 -> ServoCommand = 1 if ReadByte = 0b00110010 -> ServoCommand = 2 if ReadByte = 0b00110011 -> ServoCommand = 3 else printf("invalid command sent")
I am understanding this correctly?