Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] microcontrollers

Status
Not open for further replies.

tinkerer73

Member level 3
Member level 3
Joined
Apr 5, 2011
Messages
65
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,892
OK, so I'm not a complete newbe with programming, but I don't have much experience in C.
I'm also not new at electronics, but I've never created a circuit using a microcontroller before.

I have a circuit which will be controlling multiple segments of LEDs with PWM at different duty cyles using a PIC16F628A microcontroller.

From what I understand (and please correct wherever I'm wrong here!), there are two output registers, PORTA and PORTB that control multiple pins labeled as RA1, RA2, etc. and RB1, RB2, etc....To activate a pin (thereby activating the appropriate LED), I need to have my program activate the appropriate bit for each pin. There is where I'm lost. I need to know how to tell my program to activate or deactivate a bit in that register. Many years ago, I used to program in assembly, but was by no means ever a pro. IIRC, I had to use the carry bit set or clear and a bit rotator command to set individual bits, but I don't know how to go about it in C.

Also, for the same project, I'll need to know how to check status of input pins as on/off.

I greatly appreciate any help this community can give me!
 

Re: NEWBE with microcontrollers


Code C - [expand]
1
2
3
PORTB = 0x01;
fpr(i = 0; i < 8; i++)
PORTB = PORTB << 1;



Oops... It is for


Code C - [expand]
1
2
3
PORTB = 0x01;
for(i = 0; i < 8; i++)
PORTB = PORTB << 1;

 
Last edited:


Re: NEWBE with microcontrollers


Code C - [expand]
1
2
3
PORTB = 0x01;
fpr(i = 0; i < 8; i++)
PORTB = PORTB << 1;

I'm not sure I follow this exactly. I recognize the shift, but not the FPR function.

Could you comment on this code?
 

Re: NEWBE with microcontrollers

I'm not sure I follow this exactly. I recognize the shift, but not the FPR function.

Could you comment on this code?
its "for" loop not fpr.

for(i = 0; i < 8; i++)
 
Last edited:
Re: NEWBE with microcontrollers

Hi,

Its easy enough to do what you want, you can find a great deal of help in the tutorials listed below.

Be aware that many Pic output pins default to Analogue mode so you must set them to Digital for Led use.

If you want a led start up code in Pic assembly for the 628A just say.
Thanks for the response. I'll check out the tutorials.

I'm trying to write this without having someone write anything for me, because I'm forcing myself to figure it out.
I've been refreshing my memory on AND, OR, XOR functions, and trying to remember how to toggle a single bit. I know AND will allow me to turn a single bit on, but it's turning one off without affecting the rest that I'm missing.

- - - Updated - - -

its "for" loop not fpr.
Hahaha. I didn't catch the typo, and when I searched a bunch of different places for FPR, I was getting nothing.

- - - Updated - - -

So here's my basic section for controlling the PWM on the 6 segments that will chase:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// amber 1
    if (pc < seg1)
        // set bit for *rb1* to 1;
    if (pc >= seg1)
        // set bit for* rb1* to 0;
 
    // amber 2
    if (pc < seg2)
        // set bit for *rb0* to 1;
    if (pc >= seg2)
        // set bit for *rb0* to 0;
 
    // amber 3
    if (pc < seg3)
        // set bit for *ra5* to 1;
    if (pc >= seg3)
        // set bit for *ra5* to 0;
 
    // amber 4
    if (pc < seg4)
        // set bit for *ra4* to 1;
    if (pc >= seg4)
        // set bit for *ra4* to 0;
 
    // amber 5
    if (pc < seg5)
        // set bit for *ra3* to 1;
    if (pc >= seg5)
        // set bit for *ra3* to 0;
 
    // amber 6
    if (pc < seg6)
        // set bit for *ra2* to 1;
    if (pc >= seg6)
        // set bit for *ra2* to 0;


This is inside of a loop where pc is the 8 bit counter for the PWM. seg1, seg2, seg3, etc. are the PWM duty cycle for that sepcific segment (the segments will be set to 95% duty cycle as each illuminates, and then will fade slowly, so they will not all be running on the same duty cycle)

As I mentioned earlier, I'm sure I can simply define the 6 numbers that have the corresponding bit set, then just use the AND function to turn a bit on, but to turn it off, how would I write it?
 

Re: NEWBE with microcontrollers

Thanks for the response. I'll check out the tutorials.

I'm trying to write this without having someone write anything for me, because I'm forcing myself to figure it out.
I've been refreshing my memory on AND, OR, XOR functions, and trying to remember how to toggle a single bit. I know AND will allow me to turn a single bit on, but it's turning one off without affecting the rest that I'm missing.

.


Its sometime helpful to have just a basic program to set up the chip properly for you, then you can play around with the actual 'data' code as you like.

In assembly, you can use all those logic functions, but you can also simply turn on a whole port or just a single bit with one instruction, eg use xxx instruction to turn on PORTA bit 0.
You can as easily turn off a port or bit and also use a single instruction to read the state of a port or pin.

All the Assembly instructions are detailed at the back of the 628As datasheet.

You can use MPLAB IDEs own simulator to check you code or use some of the third party simulators.

The attached zipped file is just the system set up code, if does not contain any 'user' code - so no need to open it if you don't want to see it
Will let others guide you with C if you prefer that.
 

Attachments

  • 628asetup.zip
    1 KB · Views: 104
Re: NEWBE with microcontrollers

I'm using software PWM because I will have 7 channels of PWM. It would certainly be easier to use onboard PWM.

- - - Updated - - -

Upon realizing that the input/output pins on the PIC are accessed via the two registers, I'm thinking that it makes a huge amount of sense to redesign the circuit layout so that my 6 chasing segments are all on PORTA and the simple off/dim/bright segment is on PORTB, and then I'll be using one of the pins on each for the inputs. This will make the non-chasing segment far simpler, and then I can handle the 6 chasing segments with a single register.
 

Re: NEWBE with microcontrollers

If you are connecting the common anodes or cathodes to PWM PORT pins then you need a Transistor to sink/source the current if your LEDs will be in parallel.
 
Last edited:

Re: NEWBE with microcontrollers

Yes, I'm using transistors for this.
 

Re: NEWBE with microcontrollers

As I mentioned earlier, I'm sure I can simply define the 6 numbers that have the corresponding bit set, then just use the AND function to turn a bit on, but to turn it off, how would I write it?
OK so I completely forgot about the XOR operator. Correct me if I'm wrong, but this should do the trick, correct? If I simply apply the XOR with the existing byte stored in PORTA or PORTB that I want to turn off using only that bit on, it will cancel it out and leave all 7 other bits unafected?

Like this:

Code C - [expand]
1
2
3
4
5
// amber 1
    if (pc < seg1)
        B = B | seg1bit; // set bit for *rb1* to 1;
    if (pc >= seg1)
        B = B ^ seg1bit; // set bit for* rb1* to 0;


Where B is the current value stored at PORTB, and seg1bit is the number with bit 1 on (where RB1 is in PORTB).

Does this make sense, and is it the best way to do what I'm trying to do?

At the end of the code I listed previously, with all 7 segments checked and either turned on or off, I will then store the two appropriate values in PORTA and PORTB

All of the assembly programming I used to tinker with years ago is starting to come back to me.
 
Last edited:

Re: NEWBE with microcontrollers

This should do the trick, correct? If I simply apply the XOR with the existing byte stored in PORTA or PORTB that I want to turn off using only that bit on, it will cancel it out and leave all 7 other bits unafected?

Like this:

Code C - [expand]
1
2
3
4
5
6
7
seg1 = 230; // 90% duty cycle for PWM
seg1bit = 0b00000010; // bit operator for RB1
// amber 1
    if (pc < seg1)
        B = B | seg1bit; // set bit for RB1 to 1
    if (pc >= seg1)
        B = B ^ seg1bit; // set bit for RB1 to 0


Where B is the current value stored at PORTB, and seg1bit is the number with bit 1 on (where RB1 is in PORTB).

Does this make sense, and is it the best way to do what I'm trying to do?

At the end of the code I listed previously, with all 7 segments checked and either turned on or off, I will then store the two appropriate values in PORTA and PORTB:


Code C - [expand]
1
2
3
4
PORTA = A;
PORTB = B;
pc = pc++;
return;.

 

Re: NEWBE with microcontrollers

Yes XORing a variable with itself clears the variable. It is ofen used in x86 asm like XOR EBx, EBX to clear the register.
 

Re: NEWBE with microcontrollers

I got my circuit working. I set it up slightly different than originally planned, and I may go back and redo it after a few other projects. I set up a PICkit 6 pin interface on my circuit so that I can update the software if/when I want to.

Thanks to all that helped.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top