Help My Project(12CH LED Driver)??

Embedded Systems Topics
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Help My Project(12CH LED Driver)??

Post by SevenZero » Tue May 15, 2012 12:45 pm

Nandika, Go through the ADC with variable resistor path. That's the professional way of approaching this. Variable clock is only good for theoretical arguments but practically it is completely impossible to achieve anything with that. Semi says about SPI, but SPI also needs an accurate clock. Jerky clocks will tend to disrupt communication and introduce lot of instability to overall system.

I'll tell you an example. On professionally designed circuits, have you noticed the oscillator is soldered as much as closed to microprocessor. The reason is to avoid even the small interferences on long copper lines.

In such a scenario, think of having a variable resistor to control the system clock. That's simply not recommended at all. I'm sure nobody who has experience on this subject will even discuss about that ;)
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Help My Project(12CH LED Driver)??

Post by SemiconductorCat » Tue May 15, 2012 1:26 pm

In such a scenario, think of having a variable resistor to control the system clock. That's simply not recommended at all. I'm sure nobody who has experience on this subject will even discuss about that
Variable Resistor to calibrate clock! AGREED that's a bad idea. because that's a resistor and , resistance is
at a high value , and noise is proportional to the root of the temperature and resistance, that will probably
skip the time.

Image
^- last equation E is the noise voltage. T is temperature in Kelvin , R is resistance in Ohms.Don't
ask me more on this simply I don't know. I'll add bibliography , so it will be useful for a knowledge seeker.


A good expensive crystal [32.XXXMhz ] clock crystals , will have about 2ppm per Centigrade.But it varies
due to other parameters too. So even a expensive clock crystal will skip about 1 second per month.

The ultimate solution is to get the time from Seattle [NOT JOKING] , GPS signals.

Or else you need to use a crystal oven to keep the crystal temperature stable.

More than that you need accuracy, Buy a SMD atomic clock.

So even the SMD atomic clock is at lest 100$ [used in high military applications radar , missile controls,etc]
it's not suitable here. Neither you could have a crystal oven. But there is a certainly workaround on this.
You could simply read the temperature and using a simple lookup table and calibrate your RTC module.
There are 16FXXX chips with internal RTC module build in. If your building a clock then as seven zero said,
completely remove the idea of calibrating the clock with Variable Resistor.

Nadika: what actually you want to do with that variable resistor?
Semi says about SPI, but SPI also needs an accurate clock. Jerky clocks will tend to disrupt communication and introduce lot of instability to overall system.
How it's impossible ? , SPI is synchronous isn't it? That's why you could read SD card using a microcontroller
which runs low speed.
In such a scenario, think of having a variable resistor to control the system clock. That's simply not recommended at all. I'm sure nobody who has experience on this subject will even discuss about that
Completely agree with you. The only applications of controlling system clock is for reduce the power consumption
of the microprocessor.
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Help My Project(12CH LED Driver)??

Post by SevenZero » Tue May 15, 2012 6:28 pm

Semi says about SPI, but SPI also needs an accurate clock. Jerky clocks will tend to disrupt communication and introduce lot of instability to overall system.
How it's impossible ? , SPI is synchronous isn't it? That's why you could read SD card using a microcontroller
which runs low speed.
SPI.png
SPI.png (4.69 KiB) Viewed 8635 times
who's going to generate that SCLK clock? ;)

Still not clear? See this timing diagram.
SPI_timing_diagram.png
SPI_timing_diagram.png (32.66 KiB) Viewed 8635 times
See how precision timing is required to move data across devices. I'm not going to explain further. Just read about SPI.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Help My Project(12CH LED Driver)??

Post by Nandika » Sun May 20, 2012 1:31 pm

Hi Friends,
I created 8 LED flasher with speed controlling.I used VR to change speed.
I have coding small ADC Example.It work 90% correctly.
But,When i change the VR,in two end of VR LED get lowest speed.(till analog input pin[RA2] goes +5V and GND)
It's speed is not monotony.(I used VR for "POT-HG" Proteus component.Is it may be a reason for this? :idea: :?: )
Actually,What is the reason for this?

Coding

main.c

Code: Select all

    #include <pic.h>
    #include "delay.h"
    #include "adc.h"

	main(){
	PORTC=0;
	TRISB=0;
	PORTB=0x01;
	DelayMs(2000);

		do{
			int sp=GetADCRes();
			if (PORTB==0x80){
				PORTB=0X01;
				DelayMs(sp);	
			}
			else{
				PORTB=PORTB*2;
				DelayMs(sp);
			}
		}while(1);
	}
adc.h

Code: Select all

#include <pic.h>
#include "delay.h"

	int GetADCRes(){
		ADCON0=0b10001101;//Conversion speed=Fosc/32,Input channel=channel1
		ADCON1=0b10000000;//Left jestify format,alls are analog channels
		
		
		return ((1800/255)*ADRESL)+200;//min speed=2000mS,max speed=200mS,therefore [(2000-200)/255}]*ADRESL
		
	}
Circuit
circuit.png
circuit.png (4.31 KiB) Viewed 8623 times
Proteus Design File
circuit.zip
(14.76 KiB) Downloaded 377 times
Thanks All :)
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Help My Project(12CH LED Driver)??

Post by SevenZero » Sun May 20, 2012 2:57 pm

There are codes required to be called to initialise a device or a peripheral only one time. Those codes are called as init routines. To initialise ADC, you need to call such one time ADC init routine as well. I mentioned this before and you forgot to add that. Instead you add it is ADC reading code which is not correct. Here I add the init code again. Change the values as you need and call it at the beginning of main() routine. Make sure it is only called one time.

Code: Select all

// Setup A2D module
void init_a2d(void){
   TRISA  = 0x2F;   //PORTA all pins are input
   ADCON0 = 0x41;   // Select Fosc/16 and ADC on
   ADCON1 = 0xC1;   // Select right justify result.
   ADON = 1;      // Turn on the A2D conversion module
}
You can add it as this in your main() routine.

Code: Select all

main(){
        PORTC = 0;
        TRISB = 0;
        PORTB = 0x01;
        init_a2d();
        .....
        .....
The DelayMs(2000); is not required at that point.

Change your GetADCRes function.

Code: Select all

/* Return an 10-bit bit result */
unsigned short read_a2d(unsigned char channel){

   unsigned short tmpShort;
   channel &= 0x07;      // truncate channel to 3 bits
   ADCON0 &= 0xC5;         // clear current channel select
   ADCON0 |= (channel<<3);   // apply the new channel select

   // Wait till the conversion is completed
   GODONE = 1;
   while (GODONE);

   tmpShort = ADRESH;
   DelayMs(10);
   return ((tmpShort<<8)+ADRESL);
}
After reading the value from the ADC, you can convert the voltage to time as you prefer.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Help My Project(12CH LED Driver)??

Post by Nandika » Sun May 20, 2012 4:32 pm

SevenZero wrote:There are codes required to be called to initialise a device or a peripheral only one time.
I know this before.I know we must initializes ADC unit one time.
But,I want to create new .h file for include ADC function.
However,Now I can do it.

HI-TECH C not easy as I think.I like it. :) I didn't care ADON,GODON,ADIF,ADIE bits.

Thank you ZevenZero
I must study all of your advices and Data sheet. :)
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Help My Project(12CH LED Driver)??

Post by Nandika » Sun May 20, 2012 4:48 pm

Please describe some line of this code.I refereed C++ Book.but,I couldn't find those.
SevenZero wrote:

Code: Select all

/* Return an 10-bit bit result */
unsigned short read_a2d(unsigned char channel){

   unsigned short tmpShort;
   channel &= 0x07;      // truncate channel to 3 bits
   ADCON0 &= 0xC5;         // clear current channel select
   ADCON0 |= (channel<<3);   // apply the new channel select

   // Wait till the conversion is completed
   GODONE = 1;
   while (GODONE);

   tmpShort = ADRESH;
   DelayMs(10);
   return ((tmpShort<<8)+ADRESL);
}

1.

Code: Select all

ADCON0 |= (channel<<3)
What is this shifting system?

2.%=, &=, |=, ^=, <<=
Please,give small explanation for those operators.
( :idea: Give me small example and explanation for each operator.I will be able to understand easy for understand me and save your time.)

Thank
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Help My Project(12CH LED Driver)??

Post by SevenZero » Sun May 20, 2012 5:48 pm

Code: Select all

ADCON0 |= (channel<<3)
What is this shifting system?
This is left shift which is used to move bits towards the left side.
This is equals to ADCON0 = ADCON0 | (channel<<3);

If you set as ADCON0 = (channel<<3); all previous values of ADCON0 will be changed to (channel<<3) which is not correct. Setting on other bits are required. So the correct way is to preserve them by using OR operator.
2.%=, &=, |=, ^=, <<=
Please,give small explanation for those operators.
I'll explain using small examples then you will understand it better.
int a = 3, b = 4;

Say a += b;
This means a = a + b;
a = 3 + 4 = 7;

Say b <<= 1;
This means b = b << 1;
b = 4 << 1 = 8;

Say a |= b; (Note that a = 7 and b = 8)
This means a = a | b;
a = 7 | 8 = 15;

Likewise ^ is for XOR, % is for Modulo, & is for bitwise AND.

Likewise a++ means, a = a + 1;
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Help My Project(12CH LED Driver)??

Post by Nandika » Sun May 20, 2012 9:49 pm

Thank you...

I could understand it correctly. :D :D :D
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Help My Project(12CH LED Driver)??

Post by Nandika » Thu Jul 26, 2012 10:57 pm

Hello my Friends....,
(I couldn't log to ROBOT.LK some past days...And,I temporary disable my project. :cry: )
However, I have a question about Shifting of HI-TECH C.

I have 12 LED outputs.First[LSBs] 8 for PORTB. Remaining 4 for PORTC.

I coding Write method like this,

Code: Select all

void Write(unsigned int val){
	PORTB=val;
	PORTC=(val>>8);//I used shifting method for get last[MSB] bit4 to PORTC.

}
But while I call this using this method,It not work correctly.

Code: Select all

/*KNIGHT RIDER TO CENTER ALL
n=number of times
del=delay
*/

void NightRiderToCenter(unsigned short n,unsigned int del){
	unsigned short i,j;
	unsigned int val;
	for(i=0;i<n;i++){
		for(j=0;j<6;j++){
			val=pow(2,j) + pow(2,11-j);
			Write(val);
			DelayBigMs(del);

		}
	}
}
But ,This work correctly,

Code: Select all

/*
Knight rider system--DARK DOT
nByn==1/2/3/4/6
n=times of loop
del==time of LED on(delay)
comment=
YOU CAN GET 6 STYLE FROM THIS METHORD DIRECTLY.
*/

void Run12346Dot(unsigned char nByn,unsigned char n,unsigned short int del){
	unsigned char f,i;
	unsigned short int val;

	switch (nByn){
	case 1:
		f=2;
		val=1;
		break;
	case 2:
		f=4;
		val=3;
		break;
	case 3:
		f=8;
		val=7;
		break;
	case 4:
		f=16;
		val=15;
		break;
	case 6:
		f=64;
		val=63;
	}
	PORTB=4095-val;
	PORTC=((4095-val)>>8);
	DelayBigMs(del);
	for(i=0;i<n;i++){
		while(RC3==1){
			val=val*f;
			PORTB=4095-val;
			PORTC=((4095-val)>>8);
			DelayBigMs(del);
		}
		if (RC3==0){
			while(RB0==1){
				val=val/f;
				PORTB=4095-val;
				PORTC=((4095-val)>>8);
				DelayBigMs(del);
			}
		}
	}
}
Where are problems???
Post Reply

Return to “Embedded Systems”