Author Topic: C Inputs & Outputs Issue( PIC18F)  (Read 1767 times)

0 Members and 1 Guest are viewing this topic.

Offline ed_reardonTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: gb
C Inputs & Outputs Issue( PIC18F)
« on: October 15, 2016, 09:41:50 am »
Hi all,


Numpty here is tearing his hair out.

All I want this code to do is run in an endless loop,  where RB0 is low I want the pulse to be faster than when RB0 is high.

Code: [Select]
#define _XTAL_FREQ 8000000  //set osc. frequency
#include <xc.h>  //include xc8 header
#define PULSE LATAbits.LATA0  //define pin RA0 as PULSE pin
#define DIR LATAbits.LATA1    //define pin AR1 as DIRECTION pin
#define SPEED PORTBbits.RB0   //define pin RB0 as SPEED pin

   
int main ()
{
    ADCON1 = 0xFF; //switch OFF Port-A ADC
    CMCON = 0xFF; //switch OFF comparitors
    TRISAbits.RA0 = 0; //define A0 as OUTPUT
    TRISAbits.RA1 = 0; //define A1 as OUTPUT
    TRISBbits.RB0 = 1; //define B0 as INPUT
   
   
    DIR = 0; //Change value to '1' for reversed motor
       while (1)  //inititalise while loop
    {
           if (SPEED == 1) {
        PULSE= ~PULSE; //Set PULSE toggle
        __delay_us(50); //Toggle pulse  SLOW
       }
           else {
               PULSE= ~PULSE;
               __delay_us(5);  //Toggle pulse FAST
           }
           }
    return 0;
}

Now I've written this all by myself (ha) and I'm very fresh at this with a combined C coding experience measured in only a few hours.

Why does the RB0 show as a Digital OUTPUT in the MPBLab simulator?

Yours, expecting he's done something stupid!

Ed.
« Last Edit: October 15, 2016, 09:55:24 am by ed_reardon »
 

Offline Ammar

  • Regular Contributor
  • *
  • Posts: 154
  • Country: au
Re: C Inputs & Outputs Issue( PIC18F)
« Reply #1 on: October 15, 2016, 09:46:49 am »
Perhaps I am missing something, but it looks as if you have set RB0 to be an input by setting the TRIS register value bit to one. If you want it to be an output, set it to zero.
 
The following users thanked this post: ed_reardon

Offline ed_reardonTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: gb
Re: C Inputs & Outputs Issue( PIC18F)
« Reply #2 on: October 15, 2016, 09:54:49 am »
Sorry,  I mistyped and confused the living lights out of myself.

RB0 shows as a digital OUTPUT in MPBlab simulator!

Sorry for the confusion, I'll edit the initial post!

Cheers,
Ed
« Last Edit: October 15, 2016, 10:09:10 am by ed_reardon »
 

Offline ed_reardonTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: gb
Re: C Inputs & Outputs Issue( PIC18F)
« Reply #3 on: October 15, 2016, 10:27:24 am »
The plot thickens, I've noticed the emulator maps this pin to a PWM status, not a basic I/O status.

I'm guessing (and I'm very fresh to this) I need to swtich the pin to digital duty rather than PWM.

I shall RTFM!

Cheers,
Ed
 

Offline ed_reardonTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: gb
Re: C Inputs & Outputs Issue( PIC18F)
« Reply #4 on: October 15, 2016, 10:51:00 am »
Think I've got it cracked,  added PWMEN1 = 0xFF to the program.

Well if this works I'm very impressed that I've diagnosed it myself, and understood enough of the datasheet to work out what I'm doing!

Off to flash it and plug it in!

Cheers,
Ed
 

Offline ed_reardonTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: gb
Re: C Inputs & Outputs Issue( PIC18F)
« Reply #5 on: October 15, 2016, 06:48:02 pm »
Well I solved my own problem, and as such I've managed to take a product and modify it to do what I want to do by writing code and stuff!

Hey, I know this is clown shoes stupid but coming after a few hours with zero knowledge and looking online for bits and pieces I'm chuffed!  Now to rewrite the interface software....

Well maybe not!

To clarify the header should have the following code added in the main program:

PWMEN1 = 0xFF; //switch OFF PWM module

Cheers,
Ed.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf