URS - LV11 - HMI - 3. zadatak - Servo

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h" 
00002  
00003 Serial nextion(p28, p27); 
00004 PwmOut servo(p21);            
00005  
00006 char buffer[20]; 
00007 int znak = 0; 
00008  
00009 uint8_t pozicija = 0;
00010  
00011 void Rx_interrupt(void){
00012     char c;
00013  
00014     if(nextion.readable()) {
00015         c = nextion.getc();
00016         buffer[znak] = c;
00017         znak++;
00018         if ((znak >= 2) && (buffer[znak-1] == 0x0d)) {  
00019             pozicija = 0;
00020             for(int i = 0; i < 3; i++) {
00021                 if(buffer[i] == 0x0d) break;
00022                 pozicija = pozicija * 10 + (buffer[i] - 48);
00023             }
00024             servo.pulsewidth_us(5.56*pozicija+1000);
00025             memset(buffer,0,strlen(buffer));
00026             znak = 0;
00027         }
00028     }
00029 } 
00030  
00031 int main(){
00032     nextion .attach(&Rx_interrupt, Serial::RxIrq); 
00033     servo.period(0.02);    
00034     while (1) {
00035         wait(1.0);     
00036     }
00037 }