Problem in UART Library using P16F877A

Status
Not open for further replies.

ahmedabosriaa

Newbie level 5
Joined
Nov 26, 2012
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,337
Hi guys

am trying to detect data coming from USB and if it equal (1) the PROTB.RB1 go high.
but i can't figure out the problem here.

the attach contains The simulation & Code file.

Thanks In Advance.
 

Attachments

  • P16F877A.rar
    63.6 KB · Views: 116

Have a look at the attached file:-
Its working fine:-
View attachment P16F877A.zip

If you are using MAX232, change your virtual terminal setting to inverted from normal.
And in Proteus you have to specify the crystal frequency.

And its not USB ,its UART
 
Hi guys

am trying to detect data coming from UART and if it equal (1) the PROTB.RB1 go high.
but i can't figure out the problem here.

the attach contains The simulation & Code file.

Thanks In Advance.

Hi all thanks for help

I have another question ... Why this code not working !!!
PS: the simulation is attached

Code:
int uart_rd;
int i=0;

void main() {
     ADCON1=0b111110;
     PORTC=0;
     PORTB=0;
     TRISB=0b000001;
     CMCON=7;
 
 UART1_Init(9600);               // Initialize UART module at 9600 bps
 Delay_ms(100);                  // Wait for UART module to stabilize



 while (1) {                    // Endless loop
    if (UART1_Data_Ready()){     // If data is received,
       uart_rd = UART1_Read();     // read the received data,
       if(uart_rd == 123){
        PORTB.RB1=1;
       }
       else if(uart_rd == 321){
        PORTB.RB1=0;
       }
    }
  }
}
 

Attachments

  • P16F877A.rar
    63.6 KB · Views: 106

TRISC has to be set to 0x80. If you are sending ascii data like 123 then it will be actually "123" so uart_rd cannot hold "123". It can hold only one character like 1 or '1'. You have to use UART1_Read_Text().
 
Okay i am using
Code:
 UART1_Read_Text(uart_rd,"s", 255);   // read input from uart

the code is very good for the numbers less than 9 but greater than that the problem take place.

1) I have to double click to run.
2) The program goes into infinite loop.


Here's the full code:

Code:
// ADC Variables
double temp_res;
const double res = 0.25;

// PID Variables
double Input, Output;
double errSum, lastErr;
double dErr,error;
unsigned long lastTime,now;
double timeChange;

// Clock Variable
int time;

// Position contral variables
double xPos,yPos,zPos;
double theta_1N,theta_2N,theta_3N;
double theta_1O,theta_2O,theta_3O;
double setPoint;
const int link_1 = 6;
const int link_2 = 4;
double d_1,DD;
double phi;

char uart_rd[7];
int Duty,x,y,_rd,flag;



// Resolution adjustment
double resAdjust(double theta_1N){

 setPoint=0;
 if(theta_1N < res){
   setPoint = res;
 }
 else{
  while(setPoint < theta_1N){
   setPoint+=res;
  }
 }
 return setPoint;
}

// Time function
void time() {

  if(INTCON.T0IF) { // TMR0 Overflow Interrupt Flag bit 1=> has overflowed &  0=> did not overflow
    time++ ; // Increase each 260us
    INTCON.T0IF = 0 ;
   }
}


// PID Computation Function
double Compute(double Input,double setPoint){

   /*How long since we last calculated*/
    now=time;
    timeChange = (now - lastTime);

   /*Compute all the working error variables*/
    error = setPoint - Input;
    errSum += (error * timeChange);
    dErr = (error - lastErr) / timeChange;

   /*Compute PID Output*/
     Output =  error +  errSum +  dErr;

   /*Remember some variables for next time*/
   lastErr = error;
   lastTime = time;
   return  error;
}

void serialPort(){
     
     for(x=0; x<7; x++){
     uart_rd[x] = '\0';      //  Initialize string with all '\0'
     }

   if (PIR1.RCIF) {    // test the interrupt for uart rx

     UART1_Read_Text(uart_rd,"s", 255);   // read input from uart
     
     PORTB.RB7=1; delay_ms(100);
     PORTB.RB7=0; delay_ms(100);
     
     flag = 1;
   }

   if(flag == 1){
      for(y=1; y<=atoi(uart_rd); y++){
       PORTB.RB3=1; delay_ms(100);
       PORTB.RB3=0; delay_ms(100);
      }
      flag = 0;
     }
          /*
     _rd = strcmp(uart_rd, "g");
     if(_rd == 0){
      Duty = 0;
      PWM1_Set_Duty(Duty);
     }
     */
 }


void main() {

     ADCON1=0b111110;
     PORTC=0;
     PORTB=0;
     TRISB=0b000001;
     CMCON=7;
     OPTION_REG.T0CS=0;
     INTCON.GIE = 1;
     INTCON.PEIE = 1;
     INTCON.TMR0IE = 1;
     PIE1.RCIE = 1; //enable interrupt.
     PWM1_Init(20000);
     PWM1_Start();
     Duty = 255;
     PWM1_Set_Duty(Duty);
     UART1_Init(9600);               // Initialize UART module at 9600 bps
     Delay_ms(100);                  // Wait for UART module to stabilize



 while(1){


      serialPort();
      time();
      theta_1N = 35;
      theta_1O = 100;
      setPoint = resAdjust(theta_1N);
      temp_res =  ADC_Read(0);
      temp_res *= 0.25;
      temp_res = Compute(temp_res,setPoint);

      // Motor Forward & Backward
      if(temp_res < 0.25){
       PORTB.RB5 = 0;
       PORTB.RB1 = 0;
      }
      else{
       if(theta_1N > theta_1O){
        PORTB.RB1 = PORTC.RC2;
        PORTB.RB5 = 0;
       }
       else{
        PORTB.RB5 = PORTC.RC2;
        PORTB.RB1 = 0;
       }
      }
     }
      theta_1O = theta_1N;
}

The problem exactly in serialPort() function.
 

Try this UART1_Read_Text(uart_rd, 's', 8);

or this UART1_Read_Text(uart_rd,"as", 8); //If you send "has" it will detect "as" but uart_rd[] will contain only 'h'. 'as' is the delimiter and it is not stored into uart_rd[].


Where is TRISC = 0x80 in the code?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…