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.

PIC16F628A and Potentiometer with JAL

Status
Not open for further replies.

Hippogriff

Member
Hi all,

I believe that I have need for a variable resistor in my project, to allow the user to change some desired function timing. But, for this question, let's assume I would simply like to increase or decrease the frequency of an LED flash that I have connected to an output pin of my PIC16F628A.

At first, I thought that I would be using A to D conversion via JAL, obviously - upon a read of the 628A datasheet, it lacks A to D capabilities! If I wanted to use A to D, seems that I'd need another PIC. That might still be the solution, but I do like using the 628A as I'm becoming more familiar with it (slowly).

Does anyone have any hints for me on how I might set one of the 628A's pins to be an input, then connect a variable resistor / potentiometer to it and somehow get that data into my JAL program so the byte variable then can change from, say, 0 to 255 (that range would be ideal obviously :)) depending on how my user alters the dial... so that I can then use that figure to control my output flash frequency?

Sounds conceptually simple, doesn't it?, but I bet - somehow - it really isn't. Any hints and tips appreciated. Apologies if this is too simple, but this is a new component for me - I've just done output so far.
 
Last edited:
Make a series R, shunt C timing network, where the series R is a pot. Drive one end of the pot via a port pin with a square-wave generated in PIC code. Hook the junction between the R and the C to a different PIC port pin configured as an input. Use code or a Timer to measure how long after you switch the output pin the input pin goes from 0 to 1 or vice versa. The time delay is proportional to the pot resistance...
 
You could make a circuit that will convert the potentiometer position directly to a periodic input to the PIC. Then use tho PIC to measure the length of time it takes for the input to change from high to low. Use this to set the LED frequency... Of course if all you want to do is change the LED flash rate like this you could just cut the PIC out entirely and use an oscillator circuit to drive the LED directly.

You might find it is easier to find a similar PIC with A to D.
 
phoenox - Yeah, the example given, "frequency of LED flash", was really just to help me illustrate the question better. I certainly need the PIC as part of all this. I'm somewhat gutted that the 16F628A doesn't come with A to D to make this as simple as I'd hope it could be.

MikeMl - to be honest, I'm very much struggling to even get my head around what you're describing as I'm new to all this. Programming is easy, electronics is hard (for me)... conceptually all that I want is...

forever loop
pin_b1 = high
delay_1s (1)
pin_b1 = low
end loop

...to become something like:

forever loop
pot_resist = ADC_read_value (pin_a1)
pin_b1 = high
delay_1s (pot_resist)
pin_b1 = low
end loop

You get the idea. Anyway, seems that I was right, this ain't gonna be as easy as I'd hoped. Maybe I go looking for another PIC... one that is in the same nice small package as the 16F628A... or maybe even smaller, if one exists.
 
The learning curve on using a Timer inside the PIC is shorter than learning how to use the A/D. Here is the basic idea. The PIC input switches from 1 to 0 as the voltage swings through ~2.5V (Vcc=5V).

Your code causes the Output pin to switch from 1 to 0 and you simultaneously clear the Timer register. You set the Timer to count the CPU clock until the input goes from 1 to 0. The value in the Timer register is proportional to the Pot resistance.
 

Attachments

  • Draft46.png
    Draft46.png
    38 KB · Views: 864
Last edited:
Your code causes the Output pin to switch from 1 to 0 and you simultaneously clear the Timer register. You set the Timer to count the CPU clock until the input goes from 1 to 0. The value in the Timer register is proportional to the Pot resistance.

You dont even need the timer/counter just count in software how long it takes to go to a 1. Using the timer /counter might overflow too easily as it is only 8 bits.
 
You dont even need the timer/counter just count in software how long it takes to go to a 1. Using the timer /counter might overflow too easily as it is only 8 bits.

Timer 1 is 16 bits. The path from the CPU clock to Timer1 can be prescaled by 1,2,4, or 8.
The 16bit value in Timer1 can be latched into CCPR1H:CCPR2H on a down-edge on the RC2/CCP1 pin.

Or you can do it all in software...
 
Seems that I am going to have to have a proper think about my overall approach here... either extra components or a different PIC. I will try to report back on my conclusion.
 
If you're used to the 16F628 then switching to the 16F88 should be an easy step to make. The 16F88 is the same pin out but has ADC built in.

Mike.
 
The 16F88 does look pretty good, yes. It's 8MHz as opposed to 4MHz, too. I guess I should probably look to ensure that all the JAL stuff would work fine with it, and check out the prices. Intially - searching via Rapid - seems to infer they're just a little bit more expensive than what I was paying for my collection of 16F628As, but not too much. Not enough to put me off if they allow me to do what I want nice and eloquently.

Looks promising.
 
If you want an easy to learn / use language then may I recommend Swordfish BASIC and any 18F series microcontroller, the 18F1320 would be a good start IMHO.
 
I've actually had some success with the 16F628A previously and have enjoyed what I've done with JAL. I'm certainly a novice, but I'm not at the point where I am learning a language, as such, well - I'm a fairly competent programmer, is what I mean - although JAL is not something I'm expert at. JAL seems to do what I want at the moment, the biggest issue (criticism?) I would point at it would be lack of stuff on the Internet. The Yahoo mailing list is... OK.

I'm eager to try the 16F88 as I now would like to add a potentiometer to a project and use the A to D features (which the 16F628A doesn't have) instead of adding other components, as helpfully described earlier in this thread.

I'm comparing the 16F88 and the 18F1320 side-by-side right now, at:

MICROCHIP|PIC16F88-I/P|8BIT FLASH MCU, 16F88, DIP18 | Farnell United Kingdom

...and...

MICROCHIP|PIC18F1320-I/P|8BIT FLASH MCU, 18F1320, DIP18 | Farnell United Kingdom

...and, apart from the fact that the 18F1320 is more expensive, there doesn't seem to be much separating them that I should currently care about. Unless I'm missing something?

That said, the 18F1320 does have 4 PWM channels and it is PWM than I am actually doing, with an RGB LED, although I am doing it all via software in JAL at the moment. They look to have the same 8MHz internal oscillator, is that right?

Is there a special reason that you suggest the 18F1320 over the 16F88? Happy to listen to advice, it's all the same physical sized package and the cost differential is not so important... but the 18F1320 might seem like overkill to me right now... unless I really wanted those PWM channels.

P.S. - ah, I think I realised why Bill recommended the 18F1320, it's because Swordfish BASIC only supports 18XXXX PICs, not 16XXXXX PICs.
 
Last edited:
A 12f683 would be just right for an analog/pot input and software RGB pwm. These are handy for lots of small projects. The 18f1320 has just two hardware pwm's; you would have to go the 18f1330 or say 16f7X7's for three hardware pwm's. See Microchip Maps.
 
Yes, the 12F683 looks a right dandy for what I'm up to... and I have one, too. Even if I cannot get this potentiometer stuff working, I really should 'port' (if that's the right word) my 0.4.60 JAL stuff currently running on the 16F628a to JAL 2.41 and the 12F683. Even just doing that successfully would have benefits in itself.
 
OK,

I now have the 12F683 (JAL 2.41) doing exactly what the 16F628a (JAL 0.4.60) was doing. Time to start looking at using this A to D stuff with the 12F683. Good pointer, nickelflippr, it's appreciated. Changing PIC was certainly the way to go, but I never thought going down (if that's the right phrase to use) would be the solution.

Porting the JAL code from 0.4.60 with the 16F628a to 2.41 with the 12F683 was surprisingly straightforward. Not too much more than a simple copy-and-paste into the provided "blink an LED" JAL template.
 
Glad it works for you, next stop is 10f's....hehe. Not in the market for a new compiler/language, but after a quick look; Jal's syntax, supported devices, and library's seem very reasonable:). I'm impressed that someone has gone to the trouble of porting a USB module.
 
Couldn't you make an RC circuit and use the comparator of the 628 to detect it? That seems the most simple to me.
 
Glad it works for you, next stop is 10f's....hehe. Not in the market for a new compiler/language, but after a quick look; Jal's syntax, supported devices, and library's seem very reasonable:). I'm impressed that someone has gone to the trouble of porting a USB module.

Well, the 10F series is the same physical size, so there's no benefit there, of course; but they are cheaper. I think I'll stick with the 12F683 for the time being. I'd like to solder-up one of my circuits and get it all working to my satisfaction, as I have done numerous times with the 16F628a.

I found picking JAL up pretty easy, once past all the hardware-specific nomenclature that's in there. I spent a lot of my earlier life with 8086 assembler and, at the moment, just don't have the heart to learn PIC assembler. JAL 2 certainly seems better organised than JAL 0.4.60.

Couldn't you make an RC circuit and use the comparator of the 628 to detect it? That seems the most simple to me.

I really don't know. As I'm unsure of what is being suggested, you see? I'm just starting out. I think it is what was suggested earlier though, but I'd like to avoid further components if possible. That said, I'm not so bugged by the lack of A to D in the 16F628a now, as I'm much happier with the 12F683.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top