Sorry I've not been around much, been on holiday, and very very busy with a new project.
I posted a while back I was given a Picdem.net2 board to play with.
Well.............. to cut a long story short, the progress I made getting a site working on it and the site interacting with physical hardware
has meant Ive been given a contract to help develop a project on it
BUT ..... this meant learning C, specifically C18 which the Microchip TCPIP stack is written in
However ... I'm getting on really well with it.
Seeing as its not quite as user friendly as Swordfish although similar in structure it has forced me to face my biggest hurdles/fear of:-
A: Correctly understanding and working out clock cycles
B: Using and configuring Timers
C: Using and understanding Interupts
All of which after a good week of thrashing have now clicked into place ... I'm over the moon
This is a sample code I wrote myself in C18 to get a timer to run a simple clock that could be displayed on an LCD
The way of counting I used is from Graham over at Digital DIY, scaling the ms by 1000 then deducting 10000 and leaving a carry over.
I could have done this a different way, but I thinks its so well done I had to use it here
You can see how much Swordfish is like C
I just wish using things like LCD's were as easy in C though. I have yet to get the inlcuded xlcd modules in C18 to work with a single LCD
set up ... This is probably down to my Noobness but I've found a lot of people with the same LCD problems.
What makes the above funny is that I can get my GLCD running perfectly
Anyway heres my working code... As usual any tips comments appreciated
Code: Select all
// Timer0 Test Routine //
#include <p18f4520.h>
#include <timers.h>
#pragma config WDT = OFF, OSC = HS
// Declare Variables Used
#define LEDTRIS TRISBbits.TRISB3 // Led Tris
#define LEDIO LATBbits.LATB3 // Led IO Pin
int ms;
int sec;
int min;
int hr;
int tick;
void introut (void)
{
INTCONbits.TMR0IF = 0; // Clear interupt flag
ms = ms + 1638; // Inc ms by 1.638 scale by 1000 for ease of use
if (ms >= 10000) ms = ms - 10000, tick = tick +1; // deduct 10000 from ms Var and leave carry over
}
void inittimer (void)
{
//Setup Timer0
T0CONbits.T0PS0 =0;
T0CONbits.T0PS1=0; // Prescaler set to 1:32
T0CONbits.T0PS2=1;
T0CONbits.PSA=0; // Timer Clock Source is from Prescaler ( 0 = prescaler enabled )
T0CONbits.T0CS=0; // Prescaler gets clock from FCPU ( 20mhz / 4 = 5mhz )
T0CONbits.T08BIT=1; // 8 BIT MODE
INTCONbits.TMR0IE=1; // Enable TIMER0 Interrupt
INTCONbits.PEIE=1; // Enable Peripheral Interrupt
INTCONbits.GIE=1; // Enable global interrupt
}
void main (void)
{
//Declare Variable values
ms = 0;
sec = 0;
min = 0;
hr = 0;
tick = 0;
// Initialiase main program
inittimer();
LEDTRIS = 0;
LEDIO = 0;
TIMER_INT_ON;
T0CONbits.TMR0ON=1; // Start Timer
ADCON1 = 7; // Set All Digital
while(1)
{
if (INTCONbits.TMR0IF == 1) // If timer 0 hits 255 then goto interupt
introut();
if (tick >=100)
sec = sec + 1, tick = 0, LEDIO ^=1; // 100 tick = 10ms * 100 = 1 second
if (sec >= 60)
min = min +1, sec = 0;
if (min >= 60)
hr = hr +1, min = 0;
if (hr >=24)
hr = 0;
}
}