PIC16F887 Serial Reception

Discussion in 'The Projects Forum' started by josephgebran, Jun 5, 2011.

  1. josephgebran

    Thread Starter New Member

    Jun 5, 2011
    10
    0
    Dear All, I am working on a project that consists of a RC CAR controlled over the internet by WiFi. I am using PIC16F887 and the Control Interface on the PC is VB.NET . I take data into the PIC through Serial RS232. when i Use ISIS professional to simulate the PIC with a simple LED and i link ISIS and VB.NET , the PIC receives Data but nothing happens and the LED doesn't turn on, so i thought there is something wrong in my code .( i am not very smart in microcontroller programming , here is a part of the code, please help)

    void main()
    {
    again:
    init();
    Reception_loop:
    if(OERR==1)
    {RCSTA=0b10000000;
    goto again;
    }
    if(RCIF==1)
    {x1=RCREG-30;
    x2=RCREG-30;
    }
    else
    goto Reception_loop;

    The x1= rcreg-30 and x2=rcreg-30 i thought i had to write -30 because VB sends String type . and the PIC receives 2 characters one after the other, so i save them in x1 and then x2.
    In the init() function i wrote:

    RCSTA=0b10010000;
    TXSTA=0b00100100;
    BAUDCTL=0b00001000;
    SPBRG=832;

    please try to help me
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    Firstly, welcome to the Forum.
    Secondly, please put your code inside "Code" tags (#)

    You haven't included all the code - there mar/are be important bits missing.

    Did you really mean to call
    Code ( (Unknown Language)):
    1. init()
    every iteration of the loop - I suspect not.

    A programming tip - get rid of those Goto statements, a little bit of redesign work and you should be able to do it (try drawing a flowchart of what you want to happen).
     
  3. josephgebran

    Thread Starter New Member

    Jun 5, 2011
    10
    0
    Yes i call init() everytime, but you are right maybe it's dumb, but i read that if OERR is set, i mean if i haven't read RCREG and a third character comes, it will be lost, and error will be generated, and i won't be able to read characters again until i clear the OERR and this OERR is cleared when we clear CREN or SPEN , but i want them set not clear !! so how should i make a condition to read coming information without having the OERR problem ? i will post the full code in a few minutes
     
  4. josephgebran

    Thread Starter New Member

    Jun 5, 2011
    10
    0
    Code ( (Unknown Language)):
    1. #define _XTAL_FREQ 8000000
    2. #define  TMR2_PRESCALER 4
    3.  
    4. __CONFIG(0x3CC4);    // config word 1
    5. __CONFIG(0x3eff);    // config word 2 (in sequence)
    6.  
    7.  
    8. #define byte unsigned char
    9. #define word unsigned int
    10. #define dword unsigned long
    11. #define  SENSOR1 5
    12. #define Numsteps 4
    13.  
    14. char step[]= {5,9,10,6};
    15. char index=0;
    16. char pos=10;
    17.  
    18. typedef union _word_VAL
    19. {
    20.     word Val;
    21.     struct { byte LB; byte HB; } byte1;
    22. } word_VAL;
    23.  
    24. int x1,x2,x3,speed;
    25. byte ccpr1l_copy,ccpr2l_copy;
    26. void PWM1_Init(word x),PWM1_Set_Duty(byte x),PWM1_Start(),PWM1_Stop();
    27. void PWM2_Init(word x),PWM2_Set_Duty(byte x),PWM2_Start(),PWM2_Stop();
    28. void init();
    29. void Connection_done();
    30. void Reset();
    31. void fault();
    32. void Move_Forward(int speed);
    33. void Move_Backward();
    34. void Clutch();
    35. void Change_FlashLight();
    36. void Horn();
    37. void Reset_Horn();
    38. void Change_Cam_Status();
    39. void Rotate_Right();
    40. void Rotate_Left();
    41. void No_Rotate();
    42. void Rotate_Cam_Left();
    43. void Rotate_Cam_Right();
    44. void No_Cam_Rotate();
    45. void inc_index();
    46. void dec_index();
    47. void PulseMotor();
    48. byte adc_read8(byte channel);
    49. word adc_read10(byte channel);
    50.  
    51. void main()
    52. {
    53.     again:
    54.     init();
    55.  Reception_loop:
    56.                 if(OERR==1)
    57.                     {RCSTA=0b10000000;
    58.                     goto again;
    59.                     }
    60.                 if(RCIF==1)
    61.                   {x1=RCREG-30;
    62.                     x2=RCREG-30;
    63.                    }
    64.                 else
    65.                   goto Reception_loop;
    66.  
    67. x3 = x1 & 224 ;  //  to specify the speed of the robot ( XXX0 0000 )
    68. switch(x3)
    69.  { case 32:
    70.          speed = 1 ;
    71.          break;
    72.    case 64:
    73.          speed = 2 ;
    74.          break;
    75.    case 128:
    76.          speed = 3 ;
    77.          break;
    78.    default:
    79.          speed = 0 ;}
    80.  
    81.  
    82. x3 = x1 & 24 ;   // to specify the direction of rotation ( 000X X000 )
    83. switch(x3)
    84.  { case 8:
    85.          Rotate_Left();
    86.          break;
    87.    case 16:
    88.          Rotate_Right();
    89.          break;
    90.    case 24: // in case both buttons left and right are pressed at same time
    91.          break; // do nothing
    92.    default:
    93.          No_Rotate();
    94.          break; }
    95.  
    96.  
    97.  x3 = x1 & 7 ;   // to specify the direction of rotation ( 0000 0XXX )
    98. switch(x3)
    99.  { case 0:
    100.          Reset();
    101.          break;
    102.    case 1:
    103.          Move_Forward(speed);
    104.          break;
    105.    case 2:
    106.          Move_Backward(speed);
    107.          break;
    108.    default:
    109.          Clutch();
    110.          break;}
    111.  
    112.  
    113.  x3 = x2 & 1 ;
    114.    if ( x3 == 1 ) Change_FlashLight();
    115.  
    116.  x3 = x2 & 2 ;
    117.    if ( x3 == 2 ) Horn();
    118.    else Reset_Horn();
    119.  
    120.  x3 = x2 & 4 ;
    121.    if ( x3 == 4 ) Change_Cam_Status();
    122.  
    123.  x3 = x2 & 24 ;
    124. switch( x3 )
    125.  { case 8:
    126.          Rotate_Cam_Left();
    127.          break;
    128.    case 16:
    129.             Rotate_Cam_Right();
    130.             break;
    131.    default:
    132.             No_Cam_Rotate();
    133.             break;}
    134.  
    135. goto Reception_loop;
    136.  
    137. }             // End main program
    138.  
    139. void init()
    140. {
    141. OSCCON=0b01110000;   //frequency = 8M
    142. ANSEL=0b1110000;     //Enable PortA Analog Input,AN4,AN5,AN6
    143. ANSELH=0;            //Disable PortB Analog Input
    144. C1ON=0;              //turn off
    145. C2ON=0;              //comparators
    146. TRISA=0b11110000;    //RA0,RA1,RA2,RA3 Digital Outputs for stepper motor of Camera,AN4 Analog Input for Light Sensor
    147. TRISB=0b11101011;    //RB2,RB4 Digital Outputs for Horn and Flashlight
    148. TRISC=0b11110000;    //RC0,RC3 Digital Outputs for DC motor, CCP1,CCP2 for PWM Output
    149. TRISD=0b11000000;    //RD0,RD1,RD2,RD3 Digital Outputs for stepper motor of Robot ,RD4 Digital Output for Camera,RD5 Digital Output for Clutch
    150. T2CON=0b00000001;    // prescaler=4
    151. CCP1CON=0b00001100;
    152. CCP2CON=0b00001100;
    153. CCPR1H=CCPR1L=CCPR2H=CCPR2L=0;
    154. ccpr1l_copy=ccpr2l_copy=0;
    155. PWM1_Init(5000);   //Initialize PWM modules at
    156. PWM2_Init(5000);   //5KHz frequency
    157. RCSTA=0b10010000;
    158. TXSTA=0b00100100;
    159. BAUDCTL=0b00001000;
    160. SPBRG=832;
    161. }
    162.  
    163. void Connection_done()
    164. { Transmission_loop:
    165.         if ( TXIF == 0 )
    166.                 goto Transmission_loop;
    167.         else
    168.         TXREG = 2; }
    169.  
    170. void fault()
    171. { Fault_loop:
    172.         if ( TXIF == 0 )
    173.                 goto Fault_loop;
    174.         else
    175.         TXREG = 5; }
    176.  
    177. void Reset()
    178. { RD5=0;
    179.   RC0=0;
    180.   RC3=0;
    181.   PWM1_Stop();
    182.   PWM2_Stop();
    183.   }
    184.  
    185.  
    186. void Move_Forward(int speed)
    187. { byte current_duty;
    188.         switch(speed)
    189.         { case 1:
    190.             current_duty = 51;
    191.             break;
    192.           case 2:
    193.             current_duty = 127;
    194.             break;
    195.           case 3:
    196.             current_duty = 203;
    197.             break;
    198.           case 0:
    199.             RC0=0;
    200.             PWM1_Stop();
    201.             break; }
    202.    if (current_duty!=0)      
    203.    
    204.     {PWM1_Set_Duty ( current_duty );
    205.    PWM1_Start();
    206.    RC0=1; }
    207. }
    208.  
    209.  void Move_Backward()
    210.  { byte current_duty1=127;
    211.          PWM2_Set_Duty( current_duty1 );
    212.          PWM2_Start();
    213.          RC3=1; }
    214.  
    215.  
    216.  void Clutch()
    217.  { RD5=1;
    218.    RC0=0;
    219.    RC3=0;
    220.    PWM1_Stop();
    221.    PWM2_Stop();
    222.    }
    223.  
    224.  
    225.  void Change_FlashLight()
    226.  { if (RB4==1 )
    227.          RB4=0;
    228.    else
    229.      RB4=1;
    230.  }
    231.  
    232.  void Horn()
    233.  { RB2=1; }
    234.  
    235.  void Reset_Horn()
    236.  { RB2=0; }
    237.  
    238.  void Change_Cam_Status()
    239.  { if( RD4==1 )
    240.          RD4=0;
    241.    else
    242.      RD4=1;
    243.  }
    244.  
    245. void inc_index(void){
    246.     index++;
    247.     if(index == Numsteps)
    248.       index = 0;
    249.     pos++;
    250.       }
    251.  
    252. void dec_index(void){
    253. //   index = (index++) % Numsteps;
    254.     if(index == 0)
    255.              index = Numsteps-1;
    256.     else index--;
    257.     pos--;}
    258.  
    259. void PulseMotor(){
    260. byte tmp;
    261.     tmp= PORTD & 0xF0;
    262.     tmp += step[index];
    263.     PORTD=tmp;
    264.      __delay_ms(50);}
    265.  
    266.  
    267. void Rotate_Right()
    268. {
    269.  //RD0:Coil 1
    270.  //RD1:Coil 2
    271.  //RD2:Coil 3
    272.  //RD3:Coil 4
    273.       while (pos==10){
    274.       inc_index();
    275.        PulseMotor();}
    276. }
    277.  
    278. void Rotate_Left()
    279. {
    280.  //RD0:Coil 1
    281.  //RD1:Coil 2
    282.  //RD2:Coil 3
    283.  //RD3:Coil 4
    284.      while (pos==10){
    285.      dec_index();
    286.      PulseMotor(); }
    287. }
    288.  
    289. void No_Rotate()
    290. {
    291.    if(pos>10) while(pos !=10) Rotate_Left();
    292.    else if(pos<10)  while(pos !=10) Rotate_Right();
    293. };
    294.  
    295. void Rotate_Cam_Left(){};
    296. void Rotate_Cam_Right(){};
    297. void No_Cam_Rotate(){};
    298.  
    299. word adc_read10(byte channel)
    300. {
    301. byte tmp;
    302. word_VAL rslt;
    303.  
    304.    ADFM=1;
    305.    tmp=ADCON0;
    306.    tmp &= 0b11000111;   // ADCS1,ADCS0,  CHS2,CHS1,CHS0,GO,ON
    307.    tmp |= (channel << 3);
    308.    ADCON0 = tmp;
    309.    __delay_us(20);
    310.    GODONE=1;
    311.    while(GODONE);   // wait for conversion complete
    312.    rslt.byte1.LB=ADRESL;
    313.    rslt.byte1.HB=ADRESH;
    314.    return rslt.Val;
    315. }
    316.  
    317. byte adc_read8(byte channel)
    318. {
    319. byte tmp;
    320.  
    321.    ADFM=0;
    322.    tmp=ADCON0;
    323.    tmp &= 0b11000111;   //VCFG,-,CHS2,CHS1,CHS0,GO,ON
    324.    tmp |= (channel << 3);
    325.    ADCON0 = tmp;
    326.    __delay_us(20);
    327.    GODONE=1;
    328.     while(GODONE);  // wait for conversion complete
    329.    return ADRESH;
    330. }
    331.  
    332. void PWM1_Init(word x)
    333. {
    334. PR2=((_XTAL_FREQ/(4*TMR2_PRESCALER))/x)-1;
    335. }
    336.  
    337. void PWM1_Set_Duty(byte x)
    338. {
    339. ccpr1l_copy=x*(PR2+1)/100;
    340. }
    341.  
    342.  
    343. void PWM1_Start()
    344. {  
    345. CCPR1L=ccpr1l_copy;
    346. }
    347.    
    348. void PWM1_Stop()
    349. {
    350. CCPR1L=0;  
    351. }
    352.    
    353. void PWM2_Init(word x)
    354. {
    355. PR2=(500000/x)-1;  
    356. }
    357.  
    358. void PWM2_Set_Duty(byte x)
    359. {
    360. ccpr2l_copy=x*(PR2+1)/100;
    361. }
    362.  
    363.  
    364. void PWM2_Start()
    365. {
    366. CCPR2L=ccpr2l_copy;
    367. }
    368.    
    369. void PWM2_Stop()
    370. {
    371. CCPR2L=0;    
    372. }
    Btw,thank you for welcoming me :D
     
Loading...