Need Help With PIC18F4580+SK40C (Rookie)

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Need Help With PIC18F4580+SK40C (Rookie)

Postby hafiz99 » Mon Jan 09, 2012 2:03 pm

i'm Only Know Basic using Of PIC18F4580

I'm Using UltraSonic Range Finder that have pin (Vcc:Trig:echo:GDN)
ardurio HC-SR04 Ultrasonic Sensor

///------------------- Programming -------------------------///
CODE: SELECT_ALL_CODE
#include <p18f4580.h>
#include <delays.h>

#pragma config OSC = HS
#pragma config LVP = OFF
#pragma config WDT = OFF
#pragma config DEBUG = OFF
#pragma config PBADEN = OFF

void detection(void);

void chk_isr(void);
unsigned char outOfRange;
unsigned int distance;
unsigned int data;

#pragma code My_HiPrio_Int=0x0008
void My_HiPrio_Int(void)
{
   _asm
   GOTO chk_isr
   _endasm
}

#pragma interrupt chk_isr
void chk_isr(void)
{
   if(PIR1bits.TMR1IF == 1)        // timer1 overflow ?
   {
      PORTDbits.RD5 = 1;
      outOfRange = 1 ;            // set out of range flag
      PIR1bits.TMR1IF = 0 ;       // clear interrupt flag
      T1CONbits.TMR1ON = 0;
   }
}

void main(void)
   {
      CCP1CON = 0x05;
      T3CON = 0x00;
      T1CON = 0x00;
      TRISD = 0x00;
      TRISCbits.TRISC2 = 1;
      TRISCbits.TRISC1 = 0;
      TRISBbits.TRISB0 = 0;
      CCPR1L = 0x00;
      CCPR1H = 0x00;
      INTCONbits.GIE = 1;
      INTCONbits.PEIE = 1;
      PIE1bits.CCP1IE = 1;
      PIE1bits.TMR1IE = 1;
      PIR1bits.TMR1IF = 0;      
      while(1)
      {
         TMR1L = 0;
         TMR1H = 0;
         outOfRange = 0;
         CCP1CONbits.CCP1M0 = 1;
         PIR1bits.CCP1IF = 0;
         PORTBbits.RB1 = 1;
         Delay10TCYx(10);
         PORTBbits.RB1 = 0;
         while(PIR1bits.CCP1IF == 0);
         T1CONbits.TMR1ON = 1;
         CCP1CONbits.CCP1M0 = 0;
         PIR1bits.CCP1IF = 0;
         while(PIR1bits.CCP1IF == 0)
         {
            if(outOfRange = 1)break;
         }
         T1CONbits.TMR1ON = 0;
         data = CCPR1H << 8;
         data = data | CCPR1L;
         distance = data/145/2;
         detection();
         Delay1KTCYx(1);
      }
   }

void detection()
{
   if(distance>8 && distance<14)
   {
      PORTDbits.RD1 = 1;
      PORTDbits.RD2 = 0;
      PORTDbits.RD3 = 0;
      PORTDbits.RD4 = 0;
   }
   else if(distance>18 && distance<24)
   {
      PORTDbits.RD1 = 0;
      PORTDbits.RD2 = 1;
      PORTDbits.RD3 = 0;
      PORTDbits.RD4 = 0;
   }
   else if(distance>28 && distance<34)
   {
      PORTDbits.RD1 = 0;
      PORTDbits.RD2 = 0;
      PORTDbits.RD3 = 1;
      PORTDbits.RD4 = 0;
   }
   else if(distance>38 && distance<44)
   {
      PORTDbits.RD1 = 0;
      PORTDbits.RD2 = 0;
      PORTDbits.RD3 = 0;
      PORTDbits.RD4 = 1;
   }
   else
   {
      PORTDbits.RD5 = 1;
   }
}

///----------------------------------------------------////

Above Program It seem Not working if someone can help with this??
Even i put PORTD = CCPR1L; and moniter with LED the LED not Even change when change in distance
hafiz99
Newbie
 
Posts: 13
Joined: Mon Jan 09, 2012 1:38 pm

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby hafiz99 » Mon Jan 09, 2012 2:26 pm

There is another problem that occur and make me confuse about it
need help with the pro's...

CODE: SELECT_ALL_CODE
// ADC Program Testing
#include <p18f4580.h>
#include <delays.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = OFF
#pragma config PBADEN = OFF

#define Motor_Left_Front PORTDbits.RD0
#define Motor_Left_Back PORTDbits.RD1
#define Motor_Right_Front PORTDbits.RD2
#define Motor_Right_Back PORTDbits.RD3
#define Motor_Driver_Active PORTCbits.RC4

unsigned int Sensor1;
unsigned char L_byte, H_byte, data, result;
unsigned int data_all;

void read_adc(void);
unsigned short read_result(void);

void main(void)
{
   TRISD = 0;
   TRISAbits.TRISA0 = 1;
   ADCON0 = 0b00000101;
   ADCON1 = 0b00001111;
   ADCON2bits.ADFM = 1;
   while(1)
   {
      // For Sensor1
      ADCON0 = 0b00011001;
      read_adc();
      result=read_result();
      Sensor1 = result;
      if(Sensor1>225)
      {
         Motor_Left_Front = 1;
         Motor_Left_Back = 0;
         Motor_Right_Front = 1;
         Motor_Right_Back = 0;
//         PORTD = 0b00000101;
      }
      else if(Sensor1<200)
      {   
         Motor_Left_Front = 0;
         Motor_Left_Back = 1;
         Motor_Right_Front = 0;
         Motor_Right_Back = 1;
//         PORTD = 0b00001010;
      }

   }
}

// ADC Routine //
void read_adc(void)
{   
   ADCON0bits.GO = 1;
   while(ADCON0bits.DONE == 1);
   H_byte = ADRESH;
   L_byte = ADRESL;
   L_byte >>= 2;
   L_byte &= 0x3F;
   H_byte <<= 6;
   H_byte &= 0xC0;
   data = L_byte | H_byte;
}
unsigned short read_result(void)
{
   unsigned char result;
   result=data;
   return result;
}


when i test the program using LED there are different when using
CODE: SELECT_ALL_CODE
      Motor_Left_Front = 1;
         Motor_Left_Back = 0;
         Motor_Right_Front = 1;
         Motor_Right_Back = 0;

with
CODE: SELECT_ALL_CODE
      PORTD = 0b00000101


the different it the LED Brightness, when using PORTD = 0b00000101 the LED more brightness, the voltage between both command is different about 1.0 to 1.5v

Some of my friend said used
#define Motor_Left_Front LATDbits.LATD0
#define Motor_Left_Back LATDbits.LATD1
#define Motor_Right_Front LATDbits.LATD2
#define Motor_Right_Back LATDbits.LATD3
#define Motor_Driver_Active LATDbits.LATC4

what are different between them??? :shock:
hafiz99
Newbie
 
Posts: 13
Joined: Mon Jan 09, 2012 1:38 pm

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby ABSF » Mon Jan 09, 2012 2:44 pm

hafiz99 WROTE:i'm Only Know Basic using Of PIC18F4580

I'm Using UltraSonic Range Finder that have pin (Vcc:Trig:echo:GDN)
ardurio HC-SR04 Ultrasonic Sensor

Above Program It seem Not working if someone can help with this??
Even i put PORTD = CCPR1L; and moniter with LED the LED not Even change when change in distance


Can you at least describe what your program is doing ? Which part of your program is not working ? The ultra-sonic part or the motor control part ? And what do you suspect is wrong.......

I am sure nobody here have time to read through the whole program for you. So please help us to help you! :mrgreen:

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby hafiz99 » Wed Jan 11, 2012 12:39 am

ABSF WROTE:
hafiz99 WROTE:i'm Only Know Basic using Of PIC18F4580

I'm Using UltraSonic Range Finder that have pin (Vcc:Trig:echo:GDN)
ardurio HC-SR04 Ultrasonic Sensor

Above Program It seem Not working if someone can help with this??
Even i put PORTD = CCPR1L; and moniter with LED the LED not Even change when change in distance


Can you at least describe what your program is doing ? Which part of your program is not working ? The ultra-sonic part or the motor control part ? And what do you suspect is wrong.......

I am sure nobody here have time to read through the whole program for you. So please help us to help you! :mrgreen:

Allen


What my program doing is to detect distance using HC-SR04 Ultrasonic Sensor but it seem not working. even there are change in distance, the CCPR1 value didn't change at all, so it must have problem on hardware or it's programming. to say hardware problem maybe not, cuz the ultra sonic sensor is new one.. just try with all ultrasonic sensor that i have even with "PING))) from parallax"

i thought it was problem with the CCP1 port. there are a ways to check whether that port is working or not working???
Even i change the PIC still same...
hafiz99
Newbie
 
Posts: 13
Joined: Mon Jan 09, 2012 1:38 pm

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby ABSF » Wed Jan 11, 2012 6:52 am

What is the freq. of your crystal osc? I only know HS.
What is the distance in cm are you expecting to measure?
Where did you connect the pins trig and echo of your ultra sonic to? :|
What does the timer1 interrupt do in your program? :roll:

CODE: SELECT_ALL_CODE
void main(void)
   {
      CCP1CON = 0x05;
      T3CON = 0x00;
      T1CON = 0x00;
      TRISD = 0x00;
      TRISCbits.TRISC2 = 1;
      TRISCbits.TRISC1 = 0;
      TRISBbits.TRISB0 = 0;
      CCPR1L = 0x00;
      CCPR1H = 0x00;
      INTCONbits.GIE = 1;
      INTCONbits.PEIE = 1;
      PIE1bits.CCP1IE = 1;
      PIE1bits.TMR1IE = 1;
      PIR1bits.TMR1IF = 0;      
      while(1)
      {
         TMR1L = 0;
         TMR1H = 0;
         outOfRange = 0;
         CCP1CONbits.CCP1M0 = 1;
         PIR1bits.CCP1IF = 0;
         PORTBbits.RB1 = 1;
         Delay10TCYx(10);
         PORTBbits.RB1 = 0;
         while(PIR1bits.CCP1IF == 0);
         T1CONbits.TMR1ON = 1;
         CCP1CONbits.CCP1M0 = 0;
         PIR1bits.CCP1IF = 0;


There is no comment at all in the above code. I am not that familiar with 18F4580. Would you please have the curtesy to insert them? Or else only you alone can understand your program... :roll:

Do you have something like LCD or UART terminal to display the contents of Timer value or the distance? In other words, how did you debug your program?

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby hafiz99 » Wed Jan 11, 2012 5:05 pm

ABSF WROTE:What is the freq. of your crystal osc? I only know HS.
What is the distance in cm are you expecting to measure?
Where did you connect the pins trig and echo of your ultra sonic to? :|
What does the timer1 interrupt do in your program? :roll:

CODE: SELECT_ALL_CODE
void main(void)
   {
      CCP1CON = 0x05;
      T3CON = 0x00;
      T1CON = 0x00;
      TRISD = 0x00;
      TRISCbits.TRISC2 = 1;
      TRISCbits.TRISC1 = 0;
      TRISBbits.TRISB0 = 0;
      CCPR1L = 0x00;
      CCPR1H = 0x00;
      INTCONbits.GIE = 1;
      INTCONbits.PEIE = 1;
      PIE1bits.CCP1IE = 1;
      PIE1bits.TMR1IE = 1;
      PIR1bits.TMR1IF = 0;     
      while(1)
      {
         TMR1L = 0;
         TMR1H = 0;
         outOfRange = 0;
         CCP1CONbits.CCP1M0 = 1;
         PIR1bits.CCP1IF = 0;
         PORTBbits.RB1 = 1;
         Delay10TCYx(10);
         PORTBbits.RB1 = 0;
         while(PIR1bits.CCP1IF == 0);
         T1CONbits.TMR1ON = 1;
         CCP1CONbits.CCP1M0 = 0;
         PIR1bits.CCP1IF = 0;


There is no comment at all in the above code. I am not that familiar with 18F4580. Would you please have the curtesy to insert them? Or else only you alone can understand your program... :roll:

Do you have something like LCD or UART terminal to display the contents of Timer value or the distance? In other words, how did you debug your program?

Allen

Im using Standard SK40C form cytron so OSC it will be 20MHz
what i want from ultrasonic is when distance 1m it will perform avoiding program...
I Connect the trig pin at "PORTDbits.RD0" and Echo pin at "PORTCbits.RC2/CCP1".

My program above it Converting Form Pulsin() program from BasicStamp By doing some research. but not converting all of them.. :D
What pulsin do form my research is set CCP1 to Scan change form LOW-High signal ,when it detect High of Echo pin then it start Timer1 then it change the CCP1 to detect HIGH-LOW Signal, after it detect echo signal High-Low the timer 1 is Stop. sooo TMR1 address is storing number of tick of echo signal.. using datasheet and calculation (145 tick = 1cm). when the Timer1 is overflow it will set the sensor is out of range then break;

I dont used LCD or UART to display i only Used LED as Moniter. to check i just send TMR1H:TMR1L to PORTA:PORTB that have Connection With LED but that i see at LED No change in TMR1 address value even sensor is away or close obsticle :D :D

Using Ultrasonic with PIC18 is so hard :( now my robot only used Longrange IR as sensor :(

or if someone have PIC18 program for Ultrasonic Sensor PING))) Parallax can post here :)
soo i dont know what to do T_T
hafiz99
Newbie
 
Posts: 13
Joined: Mon Jan 09, 2012 1:38 pm

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby ABSF » Wed Jan 11, 2012 11:17 pm

CCP1CON = 0x05;

You're using capture mode to detect L-H transition on RC2, right?

I Connect the trig pin at "PORTDbits.RD0" and Echo pin at "PORTCbits.RC2/CCP1".

But I dont see any thing about RD0 in your program. Instead, from your software, the trig pulse is from RB1.

PORTBbits.RB1 = 1;
         Delay10TCYx(10);
         PORTBbits.RB1 = 0;


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby ABSF » Thu Jan 12, 2012 6:59 am

HC-SR04_chart.jpg
HC-SR04 timing chart


A short ultrasonic pulse is transmitted at the time 0, reflected by an object. The senor receives this signal and converts it to an electric signal. The next pulse can be transmitted when the echo is faded away. This time period is called cycle period. The recommend cycle period should be no less than 50ms. If a 10μs width trigger pulse is sent to the signal pin, the Ultrasonic module will output eight 40kHz ultrasonic signal and detect the echo back. The measured distance is proportional to the echo pulse width and can be calculated by the formula above. If no obstacle is detected, the output pin will give a 38ms high level signal.
Library:
http://iteadstudio.com/store/images/pro ... asonic.rar


This is the timing chart and quote are from the datasheet of the ultra-sonic.

1. I would suggest to add 2 uS of logic LOW on the Trig pin before the 10uS HIGH.

2. Your calculation on the distance in cm is not correct too. You divide the content of CCPR1 register by 72.5. The CCPR1 did not hold the amount in uS but 200nS because you're using 20MHz crystal. t=(1/(20/4))=0.2 uS

3. You said you displayed the contents of Timer1 in PORTA and PORTB using LEDs. But at which point you did it is not mentioned in your software. And I doubted the content of timer1 is correct at that moment.

4. May be you can modify your software just to detect the H-L change of Echo pin and forget about the L-H transition. From the graph, we dont know how much time is taken between the "Trig pulse end" to the "begin of Echo pulse".

5. Even if your timer1 overflow at the count of 0xFFFF, that's only 65536/5=13.107mS. Still a long way from the 38mS as suggest by the datasheet.

If my above calculation is wrong, please do let me know cos I am also learning about this HC-SR04 thing and I dont have a 18F4580 to try on my SK40C.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby hafiz99 » Fri Jan 27, 2012 7:30 pm

ermmm Maybe I try create the program again about CCP1 port i already set it correctly first setting to detect L-H then start the Timer1 then Setting The CCP1 Port to Detect H-L then stop the timer1. that programming process flow is correct.. maybe it about the Timer1 it self =.= wrong setting with it prescale value is not correct here =.=

Using Basic Stamp really easy with this :D but using PIC hahaha :D feel like to smash my head at the wall :D

i try using different prescale value and asking for help again :)
hafiz99
Newbie
 
Posts: 13
Joined: Mon Jan 09, 2012 1:38 pm

Re: Need Help With PIC18F4580+SK40C (Rookie)

Postby ABSF » Sat Jan 28, 2012 6:35 am

hafiz99 WROTE:ermmm Maybe I try create the program again about CCP1 port i already set it correctly first setting to detect L-H then start the Timer1 then Setting The CCP1 Port to Detect H-L then stop the timer1. that programming process flow is correct.. maybe it about the Timer1 it self =.= wrong setting with it prescale value is not correct here =.=

Using Basic Stamp really easy with this but using PIC hahaha feel like to smash my head at the wall

i try using different prescale value and asking for help again


Basic Stamp is using BASIC. A better example is to use a C example like Arduino here:

Arduino Ultrasonic Ranging With HC-SR04 | ITead Studio
http://iteadstudio.com/application-note ... h-hc-sr04/

I also read in the datasheet that CCP1 will send a reset signal to Timer1 when a CCP1 interrupt occurs, after transferring the contents of Timer1 registers to CCP1 registers.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 0 guests