SK40C + LCD help!!

Programmer, In-Circuit Debugger, PIC Start-Up Kit, Memory Interface...

Re: SK40C + LCD help!!

Postby chaos64 » Thu Apr 12, 2012 3:52 am

Can anyone help me with the SK40C 18F4520 Sample Code? Does the LCD example project need to be changed at all or can I load it up and work without any change in code? I noticed the 1ms delay and 100ms delay defined in code was defined using a 48MHz oscillator frequency, so I changed it according to the 20MHz oscillator I am using on the SK40C, please help.
chaos64
Newbie
 
Posts: 12
Joined: Fri Mar 30, 2012 8:01 am

Re: SK40C + LCD help!!

Postby ABSF » Thu Apr 12, 2012 9:15 am

As I dont have a 18F4520 to try out on my SK40C so I simulated the circuit on the simulator. I used the file xlcd.c to compile and the crystal is 8MHz as my PC is too slow.

4520_LCD.JPG


SK40C_C18.rar
(1.66 KiB) Downloaded 296 times


SK40C_C18.rar is the hex file that I used on my simulator. The forum doesnt allow hex file so I zipped it.

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: SK40C + LCD help!!

Postby chaos64 » Fri Apr 13, 2012 11:45 am

How can I use the hex file if I'm running at 20MHz? I would need to edit the code then run the program...
chaos64
Newbie
 
Posts: 12
Joined: Fri Mar 30, 2012 8:01 am

Re: SK40C + LCD help!!

Postby mec » Fri Apr 13, 2012 5:03 pm

Hi Allen,

May i know what simulator are you using? Just curious to know. 8-)
mec
Newbie
 
Posts: 12
Joined: Thu Sep 02, 2010 8:03 pm

Re: SK40C + LCD help!!

Postby ABSF » Fri Apr 13, 2012 8:08 pm

mec WROTE:Hi Allen,

May i know what simulator are you using? Just curious to know. 8-)


I'm using Proteus ISIS to simulate the circuit.

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: SK40C + LCD help!!

Postby ABSF » Fri Apr 13, 2012 8:23 pm

chaos64 WROTE:How can I use the hex file if I'm running at 20MHz? I would need to edit the code then run the program...


I didnt change the program when I compile it. I just use 8MHz in the simulator to run the circuit and it works.

CODE: SELECT_ALL_CODE
//=============================================================================
//   Configuration Bits
//=============================================================================
#pragma   config PLLDIV = 5         // 20 MHz crystal
#pragma   config CPUDIV = OSC1_PLL2   //
#pragma   config USBDIV   = 2         // Clock source from 96MHz PLL/2
#pragma   config FOSC = HSPLL_HS      //
#pragma   config FCMEN = OFF         //
#pragma   config IESO = OFF         //
#pragma   config PWRT = OFF         //
#pragma   config BOR = OFF         //
#pragma   config VREGEN = ON         // USB Voltage Regulator disabled
#pragma   config WDT = OFF         //
#pragma   config MCLRE = ON         // MCLR pin enabled; RE3 input pin disabled
#pragma   config PBADEN = OFF         // PORTB<4:0> pins are configured as digital I/O on Reset
#pragma   config CCP2MX = OFF         // CCP2 input/output is multiplexed with RC1
#pragma   config LVP = OFF         // Single-Supply ICSP disabled
#pragma   config ICPRT = OFF         // Dedicated In-Circuit Debug/Programming Port disabled
#pragma   config XINST = OFF         // Extended Instruction Set


From the program, it was configured to use 20MHz crystal. The clocking in 18F is quite complicated compared to 16F. It has PLL that can multiply the crystal frequency by a factor and then divide by another factor and you get 48Mhz clock from 20Mhz crystal. The important thing for LCD to work properly is the 1mS delay here.

CODE: SELECT_ALL_CODE
/********************************************************************
* Please copy this routine to userprogram.c, then uncomment it      */
/********************************************************************
*       Function Name:  Delay_1msX                                  *
*       Return Value:   void                                        *
*       Parameters:     miliseconds: amount of delay            *
*       Description:    This routine generates various delays       *
*                  needed by xlcd functions.               *
*                  For delay of 1ms (18F4550 running at 48MHz)   *
*                  Cycles = (TimeDelay * Fosc) / 4            *
*                  Cycles = (1ms * 48MHz) / 4               *
*                  Cycles = 12,000                        *
*                  Since call of function also takes some       *
*                  instruction cycles, the exact value to get   *
*                  1ms delay is less than 12,000.            *
********************************************************************/
void Delay_1msX (unsigned int miliseconds)
{
   t=0;
   while(t<miliseconds)
   {
      Delay1KTCYx(11);
      Delay10TCYx(96);
      Nop();
      Nop();
      Nop();
      Nop();
      Nop();
      t++;
   }
}                                                     //End of Delay_1msX


Any one care to explain how PIC18F PLL clock works ? And how is 1 mS delay generated ?

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: SK40C + LCD help!!

Postby chaos64 » Sat Apr 14, 2012 2:47 am

Those configuration settings look like there for the PIC18F4550, there is no PLLDIV option in the configuration settings for the PIC18F4520 I am using.

I have the 20MHz crystal that came with the SK40C installed on the OSC1 and OSC2 pins, I calculated the new delay routines to work in consideration with the .2uS instruction cycle on the 20MHz clock: 1/(20mhz/4) = .2uS

The sample code still does not work, the LCD is not faulty and all the soldering is fine. Can anyone help??
chaos64
Newbie
 
Posts: 12
Joined: Fri Mar 30, 2012 8:01 am

Re: SK40C + LCD help!!

Postby kl84 » Mon Apr 16, 2012 8:56 am

If you are using PIC18F4520, you should refer to "PIC18F Sample Source Code", not "PIC18F_USB Sample Source Code". The one I downloaded has Configuration Bits look like this:
CODE: SELECT_ALL_CODE
//=============================================================================
//   Configuration Bits
//=============================================================================
#pragma   config OSC = HS            // HS oscillator
#pragma   config FCMEN = OFF         // Fail-Safe Clock Monitor disabled
#pragma   config IESO = OFF         // Oscillator Switchover mode disabled
#pragma   config PWRT = OFF         // PWRT disabled
#pragma   config BOREN = OFF         // Brown-out Reset disabled in hardware and software
#pragma   config WDT = OFF         // WDT disabled (control is placed on the SWDTEN bit)
#pragma   config MCLRE = ON         // MCLR pin enabled; RE3 input pin disabled
#pragma   config PBADEN = OFF         // PORTB<4:0> pins are configured as digital I/O on Reset
#pragma   config CCP2MX = PORTC      // CCP2 input/output is multiplexed with RC1
#pragma   config LVP = OFF         // Single-Supply ICSP disabled
#pragma   config XINST = OFF         // Extended Instruction Set


I found that the delays are not accurate to work for PIC18F4520, like what you said, it's for PIC18F4550. Despite its inaccuracy, it still works on my board.

ABSF WROTE:Any one care to explain how PIC18F PLL clock works ? And how is 1 mS delay generated ?

Usually I do like this:
Debug.jpg

Go to Debugger > Select Tool > MPLAB SIM. Then Debugger > Settings > 20MHz and Debugger > StopWatch. I call the delay function in my main program and place breakpoints before and after this function call line. Build the program and run debugger. The debugger will cause the program to stop at first breakpoint. Zero the StopWatch, run the debugger again. When it stops at 2nd breakpoint, you will get the time Instruction Cycle and Time used to execute the delay function.

This is what I get to yield 1msec delay:
CODE: SELECT_ALL_CODE
void Delay_1msX (unsigned int miliseconds)
{
   t=0;
   while(t<miliseconds)
   {
      Delay1KTCYx(4);
      Delay10TCYx(95);
      Nop();
//      Nop();
//      Nop();
//      Nop();
//      Nop();
      t++;
   }
}//End of Delay_1msX

I have to comment some Nop(); to get the closest value to 1 msec. Delay1KTCYx(); and Delay10TCYx(); are C18 libraries functions and Nop(); does nothing for one instruction cycle.

My StopWatch shows Instruction Cycles = 5001 and Time (mSecs) = 1.000200. You can have a try to get the correct 100 msec delay for your processor and crystal frequency.
User avatar
kl84
Amateur
 
Posts: 166
Joined: Thu Jul 08, 2010 12:14 pm

Re: SK40C + LCD help!!

Postby ABSF » Tue Apr 17, 2012 7:26 am

kl84 WROTE:Usually I do like this:

Go to Debugger > Select Tool > MPLAB SIM. Then Debugger > Settings > 20MHz and Debugger > StopWatch. I call the delay function in my main program and place breakpoints before and after this function call line. Build the program and run debugger. The debugger will cause the program to stop at first breakpoint. Zero the StopWatch, run the debugger again. When it stops at 2nd breakpoint, you will get the time Instruction Cycle and Time used to execute the delay function.

This is what I get to yield 1msec delay:
CODE: SELECT ALL
void Delay_1msX (unsigned int miliseconds)
{
   t=0;
   while(t<miliseconds)
   {
      Delay1KTCYx(4);
      Delay10TCYx(95);
      Nop();
//      Nop();
//      Nop();
//      Nop();
//      Nop();
      t++;
   }
}//End of Delay_1msX
I have to comment some Nop(); to get the closest value to 1 msec. Delay1KTCYx(); and Delay10TCYx(); are C18 libraries functions and Nop(); does nothing for one instruction cycle.

My StopWatch shows Instruction Cycles = 5001 and Time (mSecs) = 1.000200. You can have a try to get the correct 100 msec delay for your processor and crystal frequency.


Thank you for the lengthy explanation of the mplab debugging tool. I should have put more time in this tool instead of just simulating the circuit.

Regards.

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: SK40C + LCD help!!

Postby chaos64 » Sun Apr 22, 2012 3:48 am

Hello I managed to get the sample LCD code to work on a different LCD wired up to the ports on the main board not the header I soldered in, but it only works when the UIC00B is plugged in to the SK40C. What is causing this?
chaos64
Newbie
 
Posts: 12
Joined: Fri Mar 30, 2012 8:01 am

PreviousNext

Return to PIC Development Tool

Who is online

Users browsing this forum: No registered users and 22 guests