Author Topic: PIC18F4550  (Read 8100 times)

0 Members and 1 Guest are viewing this topic.

Offline aboodiTopic starter

  • Contributor
  • Posts: 18
PIC18F4550
« on: April 06, 2014, 02:43:27 pm »
Hello everyone ,

I've been struggling to get the PIC18F4550 running. I firstly connected a LCD, then I realised that I can not even control a LED.
So, I tried to focus on the LED only. I tried playing around with almost everything.

thanks a lot  :-+ ,
Abdullah

datasheet of the chip: http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf

Code: [Select]
#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port function on RA6, EC used by USB (INTIO))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = OFF     // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

#define LED LATEbits.RE0

void main(void) {

    TRISA = 0xFF;
    TRISB = 0xFC; // RS & E are o/p pins
    TRISC = 0xF0; // (DB7 .. DB4) pins of the LCD r o/p pins
    TRISD = 0x0F;
    TRISE = 0x00;

    // RS = 0;
    // E = 0;
    // PORTC= 0x00;

    ADCON0bits.ADON= 0;
    ADCON1bits.PCFG= 0xF;
    INTCON2bits.RBPU= 1; // 1: port B pull-ups disabled

    UCONbits.USBEN= 0; // 0: disable USB

    SSPCON1bits.SSPEN= 0; // 0: disabling SSP module

    INTCONbits.RBIE= 0; // 0: disable interrupts on RB4 & RB5
    INTCON2bits.RBIP=1; // port B interrupt priority

    PIE1=0; // peripheral interrupt disabled 1 & 2
    PIE2=0;

    INTCONbits.GIEH= 0; // General Interrupt Enable
    INTCONbits.GIEL= 0;
    INTCONbits.GIE= 0;

    SPPCONbits.SPPEN = 0;
    UCONbits.USBEN = 0;
    UCFG = 0b00001000;
   
    RCSTAbits.SPEN = 0;

    CCP1CONbits.CCP1M = 0x0;

        while(1){

            LED = 1;
            LATD = 0xF0; // The LCD o/p pins

            __delay_ms(1000);
           
            LED = 0;
            LATD = 0x00; // The LCD o/p pins
           
            __delay_ms(1000);
           
        }
       
    return;
}
« Last Edit: April 06, 2014, 02:48:21 pm by aboodi »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC18F4550
« Reply #1 on: April 06, 2014, 03:11:02 pm »
Quote
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)

Generally, start out by turning them all off, including comparators and analog input.

================================
https://dannyelectronics.wordpress.com/
 

Offline pa2ees

  • Contributor
  • Posts: 39
  • Country: us
Re: PIC18F4550
« Reply #2 on: April 06, 2014, 03:18:05 pm »
Everything looks mostly in order.  However, I don't think LATEbits has a member named RE0.  Usually the convention is LATEbits.LATEx.  Try that and see if it works. 

In these situations, and to get a feel for the micro (because it's a fairly complex one) I usually make all ports digital output, and toggle them all, and probe each pin with my scope.  First, you find out which ones are ready to go, and the ones that are not toggling have other things associated with them, ie ADC, Secondary oscillator (SOSC), clock output, etc.

My method of troubleshooting usually consists of checking EVERY bit of code, no matter how trivial, and verifying each one works, and building on that.  So first check bit toggling, then check delay routines, then interrupts, etc
 

Offline aboodiTopic starter

  • Contributor
  • Posts: 18
Re: PIC18F4550
« Reply #3 on: April 06, 2014, 03:27:43 pm »
dannyf: I have just set this bit, and it doesn't seem to work. But, do you know the purpose of this bit?

pa2ees: Thanks for the technique you mentioned. It is really complex, I agree with that.
Regarding to the LATEbits.LATEx , i corrected that, and now there is no single pin that works for me!

That is why I used this simple code to see if the MCU actually works properly.

I found out that some pins go HIGH, then stay for around a second or about, then everything goes LOW.
What is going on? oO

Code: [Select]

#include <xc.h>

#pragma config FOSC = INTOSCIO_EC, WDT = OFF



void main(void){

    TRISC=0x00;
    TRISD=0x00;

    LATC = 0xFF;
    LATD = 0xFF;

    while(1)
        ;
           
}
 

Offline pa2ees

  • Contributor
  • Posts: 39
  • Country: us
Re: PIC18F4550
« Reply #4 on: April 06, 2014, 05:35:09 pm »
I like the new simple program.  I would make sure there is no compiler optimizations on.  I would also do something like the following:

Code: [Select]
while(1)
{
   LATC = 0xFF;
   LATD = 0xFF;

   LATC = 0x00;
   LATD = 0x00;
}

that *should* get something going.
 

Offline aboodiTopic starter

  • Contributor
  • Posts: 18
Re: PIC18F4550
« Reply #5 on: April 06, 2014, 06:40:14 pm »
I believe the issue is due to the floating pins & the breadboard! I will, however, pull the input pins down and see if the problem gets resolved.

Thanks everyone :)
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: PIC18F4550
« Reply #6 on: April 06, 2014, 06:49:24 pm »
Got a pullup on MCLR?
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline pa2ees

  • Contributor
  • Posts: 39
  • Country: us
Re: PIC18F4550
« Reply #7 on: April 06, 2014, 08:14:42 pm »
Excellent suggestion Skimask.

Also make sure that you have it configured to use the internal fast rc oscillator, so it's not waiting for an external oscillator/clock.

The floating pins shouldn't affect the micro, unless it's the MCLR that's floating.

I believe the pickit 2/3 should pull up the MCLR, unless you have told it to tri-state after programming, thought I'm not 100% sure (it's been a while).  If you are using ICD3, use the debug mode to see if the chip is actually running.
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: PIC18F4550
« Reply #8 on: April 06, 2014, 08:22:05 pm »
PK2/3 will only do the MCLR pullup if you tell it to (or tell it not to, I forget which is the case).
At any rate, I'm in the habit of always putting at least a 100K between MCLR & Vdd.

And as the O/P has figured out, the very first thing I add to a PIC when designing a board, and testing it after finishing it up, is running a 'blinky' LED.  One pin is always set aside for that purpose alone.
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Online AndyC_772

  • Super Contributor
  • ***
  • Posts: 4223
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC18F4550
« Reply #9 on: April 06, 2014, 08:46:09 pm »
The usual PIC 'gotchas' are not having a pullup on MCLR, not setting the pins as digital I/O as opposed to analogue, and incorrect clock source.

Try:

Code: [Select]
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port function on RA6, EC used by USB (INTIO))

void main (void)
{

ADCON1bits.PCFG= 0xF; // definitely need this!!

    TRISC=0x00;
    TRISD=0x00;

    while (1)
    {
    LATC = 0xFF;
    LATD = 0xFF;
    LATC = 0;
    LATD = 0;
    };
}

If it's working, the LED will blink too fast to see, of course, but you can always scope it.

Offline pa2ees

  • Contributor
  • Posts: 39
  • Country: us
Re: PIC18F4550
« Reply #10 on: April 06, 2014, 08:58:07 pm »
I'll add to those 'gotchas'

Secondary Oscillator (SOSC) pins configured as oscillator instead of I/O pins
Clock Out pin not configured as I/O
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: PIC18F4550
« Reply #11 on: April 07, 2014, 12:59:43 am »
Quote
   
Quote
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
Generally, start out by turning them all off, including comparators and analog input.
I wouldnt' turn off LVP unless you're sure that your programmer supports HVP.
However, you can add to your list of "gotchas": "LVP is enabled, but the LVP pin isn't connected."
http://www.microchip.com/forums/m640643.aspx
 

Offline aboodiTopic starter

  • Contributor
  • Posts: 18
Re: PIC18F4550
« Reply #12 on: April 07, 2014, 03:18:44 pm »
IT IS WORKING! how? I have no idea.

but I guess I was compiling a project that I am not working on! In MPLAB X, you have to set xProject as the main project. So, whenever you compile, the compiler will compile the xProject, even if you are working on another one!

Well, I still don't know if that was the problem. -.-'

I used the simple code AndyC_772 suggested, with the line "ADCON1bits.PCFG= 0xF;" corrected to be "". (Thanks man, you are a blessing!). Then as usual, I added the extra lines generated by the IDE.

The Code:
Code: [Select]

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port function on RA6, EC used by USB (INTIO))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = OFF     // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

void main(void) {

    TRISA = 0xFF;
    TRISB = 0xFF;
    TRISC = 0x00;
    TRISD = 0x00;
    TRISE = 0xFF;

    UCFGbits.UOEMON = 0;
    UCFGbits.UPUEN = 0;
    UCFGbits.UTRDIS = 0;
    UCFGbits.PPB = 0;

    ADCON0bits.ADON= 0;
    ADCON1= 0x0F;
    INTCON2bits.RBPU= 1; // 1: port B pull-ups disabled

    UCONbits.USBEN= 0; // 0: disable USB

    SSPCON1bits.SSPEN= 0; // 0: disabling SSP module

    INTCONbits.RBIE= 0; // 0: disable interrupts on RB4 & RB5
    INTCON2bits.RBIP=1; // port B interrupt priority

    PIE1=0; // peripheral interrupt disabled 1 & 2
    PIE2=0;

    INTCONbits.GIEH= 0; // General Interrupt Enable
    INTCONbits.GIEL= 0;
    INTCONbits.GIE= 0;

    SPPCONbits.SPPEN = 0;
    UCONbits.USBEN = 0;
    UCFG = 0b00001000;
   
    RCSTAbits.SPEN = 0;

    CCP1CONbits.CCP1M = 0x0;

    while(1)
{
   LATC = 0xFF;
   LATD = 0xFF;

   LATC = 0x00;
   LATD = 0x00;

}
       
    return;
}



Thanks Everyone <3
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC18F4550
« Reply #13 on: April 07, 2014, 09:46:42 pm »
Congrats. You have learned the essential steps in programming a mcu:

1. Specify the fuses: no matter if you wish to use their default values, for documentation and completeness. Tune off as many of them as you can.

2. Initiate the mcu: typically turn pins to gpio, turn off analog or alternate functions, even if you may need them later. This will greatly reduce weird behaviors later.

3. Run blinky: this makes sure that step 1 and 2 are working and you can indeed program the chip and the chip can execute your code.

I put step 1 and 2 in a header file and include it in my projects. With conditional compilation it can handle many different chips - mine is close to 100kb in size.

with that approach, you don't have to go through the datasheet to find the right fuse settings when you encounter the chip later.

code once, use forever.
================================
https://dannyelectronics.wordpress.com/
 

Offline aboodiTopic starter

  • Contributor
  • Posts: 18
Re: PIC18F4550
« Reply #14 on: April 12, 2014, 12:42:15 pm »
many thanks dannyf!

That is what I am going to do now on.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf