Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Use BCD switch to vary Frequency with a PIC microcontoller

Status
Not open for further replies.

lowski

New Member
Well, I just started learning how to write simple code in C++. I just finished going through a beginners book and I've learned the basics but I still have a lot of questions. Well I wanted to see if anyone can help write a code for a PIC that would take a BCD switch input and output the frequency. So for example if I set the BCD switch to 9 it would output 9Khz.

Thanks
 
Sorry for the confusion, KAKKAR. I have a taken course in C++ but I'm currently trying to teach myself embedded C for PIC's. I don't know much a embedded C programming but I bought of few books to help.

Ian Rogers, I'm currently using HI-TECH C compiler, and it's C language that I'm using not C++
 
Then it's quite a simple task.... Find out the timer value for each frequency required... When you select the BCD setting just change the timer to overflow at the correct intervals

When the timer overflows...toggle the output.

The easiest way to start is to choose a pic chip..... Then a frequency you want it to run at......
Grab one of the demo / example program that ships with HI-TECH...or one of my C code tutorials.

Modify to suit.
 
I'm having trouble understanding how to get the PIC to read the BCD switch through four PORT lines. So the code will have to read the state of each PORT and convert it to a binary value.
 
There is no need.... if you place the BCD switch onto.... say portb of a pic, if you use 0 through 3 you can just read the port and the number will be 0 though 16 (if you use a hex switch)

value = PORTB & 0xF; value will now contain the switch value.
 
Ian...I see what your saying now. Thanks for helping out a newbie like myself. I will let you know how it goes. By the way I'm using a PIC16F877, if you have an example of this code would you please show it to me.
 
here's where I'm at, again I'm new to this so any advice will be greatly appreciated, thanks

void main()
{
//Input from switches

//Initialization
ADCON0 = 0; // A/D ports off

PORTA = 0x00; // clear all PortA
TRISA = 0xFF; // Make all Port A Inputs

//Infinite Loop
//Loop: While 1
while (1)
{
//Get input from switch on Port A
//Input: PORT A -> SWITCHES
TRISA = TRISA | 0xff;
BCD_SWITCH = PORTA;


//Convert Switch Data to Frequency and output on PORTB...(Need help from here)
 
Ian, How do I link the input of the BCD switch and convert it into an output freq. I have looked over your suggested link but I'm completely lost. I don't want to waste your time but your help is greatly needed...thanks, belive me I'm reading through the data sheet and right now it all seems a little over whelming.
 
If you run a pic16 series micro at 4mhz, the instuction per second are 1,000,000 (million) 1 / frequency = duration so.. 1/1,000,000 = 1uS...

If you want your binary switch to select 1khz -> 16khz, your time periods will be 1mS -> 62.5uS (BUT there will be two time periods high and low)

so we need to trip the timer at 500us and 32 (you'll have to drop the 0.25 and round up), so 500uS and 31uS Timer0's smallest count (1:2) multiples of 2

So pre-load TMR0 with -16 (239) for a frequency of 16khz and -250 (5) for 1khz.


So a 16 entry lookup table with the timer re-load values

REMEMBER to adjust for size of ISR (interrupt service routine) can be about 15 clock cycles..
 
Ian, please note that the smallest increment for Timer 0 is 1 tCy (1 cycle). When you assign the prescaler to the WDT, Timer 0 is effectively running at 1:1 input ratio...

Yes Read this
6.4 Prescaler
There is only one prescaler available, which is mutually
exclusively shared between the Timer0 module and the
Watchdog Timer. A prescaler assignment for the
Timer0 module means that the prescaler cannot be
used by the Watchdog Timer and vice versa. This
prescaler is not readable or writable (see Figure 6-1)

We can only go by the datasheet... The smallest prescaler for the timer0 is 1:2
 
We can only go by the datasheet... The smallest prescaler for the timer0 is 1:2

Yes, I know. But I thought perhaps you weren't aware that if you assign the prescaler to the WDT then Timer 0 will run without the prescaler with a 1:1 input ratio.

Timer0's smallest count (1:2) multiples of 2

Perhaps I misinterpreted your meaning from this line? What were you trying to say, please?
 
Last edited:
Burt has already explained the reason for the multiple of 2.... high / low.

Huh? It seems there's a communications problem. I noticed the OP has gone to the Microchip forum. I wonder if he was having as much trouble understanding what you're trying to say as I am?
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top