Programming Pics using C

Post here if it doesn't seem to fit anywhere else.

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Programming Pics using C

Post by odessa » Fri Jun 17, 2011 11:26 pm

Hi People :D

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 :D :D :D

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 :lol:

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;



}
}
Edited to fix the LED toggle being in the wrong place
Last edited by odessa on Sat Jun 18, 2011 7:36 pm, edited 2 times in total.
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Programming Pics using C

Post by bitfogav » Sat Jun 18, 2011 3:57 am

Hi Jay, hope you had a nice holiday? go anywhere nice?

Thanks for posting the code too, I will give this ago. :)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Programming Pics using C

Post by brad » Sat Jun 18, 2011 8:00 am

has meant Ive been given a contract to help develop a project on it :D :D :D
Image

Well done! That's really great news :D

What's the contract all about or are you not able to say just at the moment?

counting clock cycles and interrupts are still something I have mostly kept away from. I do want to get into generating video games on an actual tv which will require these skills so one day I will need to learn!

Hows the user base behind C18? I may very well have to get into some C myself and we could have a new forum here just for it!

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Programming Pics using C

Post by odessa » Sat Jun 18, 2011 5:51 pm

Hi Brad,

Thx :D :D :D

I can't believe I'm going to be paid for something I love doing :lol:

I can't say exactly what it is as I had to sign a non disclosure agreement, but I can say I am in the Security business and its along that line, an access control type thing.

What do you mean by User Base ? If you mean common Libraries for functions there are plenty both included and shared by people online... Not quite as simple as say Swordfish but not hard
If you mean C help forums etc there are plenty but C differs slightly platform to platform but all follow the same syntax rules.

I really though I would hate C but I must admit I'm loving it, plus its a portable code too....


Jay
Last edited by odessa on Sat Jun 18, 2011 7:37 pm, edited 1 time in total.
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Programming Pics using C

Post by odessa » Sat Jun 18, 2011 6:05 pm

Hi Jay, hope you had a nice holiday? go anywhere nice?
Hi Gav ..... NO !!! :lol:

It was only a short break in Bournemouth, Got a chest infection and the weather sucked :(

Still, going to Portugal in August .... Can't wait :D

Jay
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Programming Pics using C

Post by brad » Sun Jun 19, 2011 8:36 pm

Well, once the product is complete be sure to post full details!

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 1 guest