Cool Engineering

Info on some cool engineering projects

Friday, September 22, 2006

Clock me up!

Just yesterday Wade and I were fiddling around with the clock settings for the control board for our project. By default an internal clock is used (generated within the AVR). This internal clock is slower than the external clock that we are using (16MHz) and so had to be changed. Unfortunately changing the clock can be a slightly confusing process - since there are about 15 settings for different external oscillators. Wade picked out one and tried it (with my approval). Note we didn't properly read the data sheet - and so after we made this change everything went dead.
The programmer (ISP) that is used doesn't generate its own clock signal - and since we were using a clock that didn't actually exist we couldn't reprogram to fix the problem. My solution was a fallback one as I didn't have any crystals about. Program a PIC microcontroller to toggle an output to emulate the clock. To do this we had to remove the old oscillator from the AVR board, connect the PIC and then reprogram the fuse bits. This worked a treat - we replaced the oscillator back in and we are in business at 16MHz.
The code for the PIC couldn’t' have been much simpler - and using the internal oscillator on the PIC16F88, I basically had to connect power to the pic with some decoupling caps and that was it. Simple!


#include
#define clockFreq 0x70 //8MHz

#ifdef _PIC16
#pragma DATA _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
#endif //_PIC16

void main()
{
osccon = clockFreq; //Choose internal clock rate
ansel = 00000000b; //all A/D off
cmcon = 00000111b; //comparators off

trisa = 00000000b;//All outputs
trisb = 00000000b;//All outputs
porta = 00000000b;
portb = 00000000b;

while (1){
porta ^= 0x01; //Invert bottom bit
}
}

2 Comments:

Anonymous Anonymous said...

Of course, Rob has glossed over some details, like how damn long it took to get the stupid clock working, even once we had reset the fuses. And then once we had it set to the full swing oscillator, it wouldn't work... after much fussing and wasted time Rob discovered that the programming speed (602.3Hz or somesuch) was apparently too slow for it... setting it up to 14kHz or somethingerrather fixed it.

Given that all indications are to use a slower clock when unsure, this was of course completely unintuitive.

Given that there are ten pins on the programmer connection, why they couldn't provide their own clock is beyond me.

10:14 AM  
Blogger Robert Ross said...

Yes, unintuitive - but it certianly programs faster now - a big bonus.
Four of the pins are grounded - it would be really handy if there was a clock pin as it would stop these silly problems ever occuring. I guess it suggests that reading the data sheet in a little more depth may be warranted (especially before changing fuse bits)

11:24 AM  

Post a Comment

<< Home