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] HELP!! Still Learning, Changing RA0-RA3 to outputs.

Status
Not open for further replies.

Hawk44

Newbie level 6
Joined
Jan 15, 2014
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
140
Just joined the forum, need some help.

I am using MPLAB 8, C18, and learning to program. I am using a PIC18F45K20 ic.

My code is a little messed up in that I have been trying different things found on the web.
I have gone through the Data sheet and it is confusing when attempting to see what relates to
what with RA0, etc. I am trying to just blink an LED off/on with the ports A. Would like to use all of
them eventually. Sure would like some help, so I can keep on learning, but I am sort of stuck now.
I am also using the demo board for the 18F45K20.
The LED's work fine on portD, But check with voltmeter shows no change on portA, When I move LED to port D, works fine. I conclude that my code is at fault. Perhaps not setting the port A to digital. Changed PBADEN = OFF //disable the analog inputs on RB0 through RB4, but didn't see anything simular for port A.


Here is my code:


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//******************************************************************************
//     
// *******************************************************************
// PIC18F45K20 PICkit 3
//
// 
//
// *******************************************************************
// *               *
// *******************************************************************
 
/** C O N F I G U R A T I O N   B I T S ******************************/
 
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                       // CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30                        // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                     // CONFIG2H
//#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H  orginal
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF                                                  // CONFIG7H
// note: PBADEN=OFF DISABLE the analog inputs on RBO THROUGH RB4
 
/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
#include "DELAY.h"
 
/** V A R I A B L E S *************************************************/
#pragma udata   // declare statically allocated uinitialized variables
unsigned char LED_Display;  // 8-bit variable
 
/** D E C L A R A T I O N S *******************************************/
#pragma code    // declare executable instructions
 
    //PORT D Setting: Set all the pins in port D to Output.
    //ADCON1=0x0F; // turn all of port A into digital I/O 
    //ADCON1=0b00000000; // turn all of port A into digital I/O 
            
void delayzz(void) 
{   int i, j;
    for(i=0;i<1000;i++)
     {
    for(j=0;j<2;j++) 
 
             {       /* Well its Just a Timer */   }}}
 
 
 
 
 
 
 
 
void main(void) 
{
 
    TRISD = 0b11111111;// Need to set as input then as an output to get output
 
    TRISA = 0b11111111;  //Need to set as input then as an output to get output
 
 
 
delayzz();
    
     //PORT D Setting: Set all the pins in port D to Output.
//  ADCON1=0x0F; // turn all of port A into digital I/O 
    //ANSEL = 0xE8; //Disables analog input.
    //ANSELH = 0x00;    // e8
    
    TRISD = 0b00000000;//  Set all the pins in port D to Output. 
    TRISA = Ob00000000; // Set all the pins in port a to Output.  
    
    
 
  while(1)    
        {
          delayzz();    
 
        Delay100TCYX(78);   // 78 x 12.8ms = .9984 s. The timing cycle runs at one-forth of the frequency.
 
        
 
        LATAbits.LATA0 = 1;    // RA-1  Blue on
        
        delayzz();
        delayzz();
        LATAbits.LATA0 = 0;    // RA-0    Blue off
 
    
        LATDbits.LATD0 = 1;   // RD-0 to High   ON 1
        delayzz();
        delayzz();
        LATDbits.LATD1 = 1;   // RD-1 to High   ON 2
        delayzz();
        delayzz();
        LATDbits.LATD2 = 1;   // RD-2 to High   ON 3
        delayzz();
        delayzz();
        LATDbits.LATD3 = 1;   // RD-3 to High   ON 4
        delayzz();
        delayzz();
        delayzz();
        delayzz();
        LATDbits.LATD0 = 0;    // RD-0 to LOW   OFF 1
        delayzz();
        delayzz();
        LATDbits.LATD1 = 0;    // RD-1 to LOW   OFF 2
        delayzz();
        delayzz();
        LATDbits.LATD2 = 0;    // RD-2 to LOW   OFF 3
        delayzz();
        delayzz();
        LATDbits.LATD3 = 0;    // RD-3 to LOW   OFF 4
        delayzz();
        delayzz(); 
        delayzz();
        delayzz();
        LATDbits.LATD4 = 1;   // RD-4 to High   ON 5
        delayzz();
        delayzz();
        LATDbits.LATD5 = 1;   // RD-5 to High   ON 6
        delayzz();
        delayzz();
        LATDbits.LATD6 = 1;    // RD-6 to HIGH  ON 7
        delayzz();
        delayzz();
        LATDbits.LATD7 = 1;    // RD-7 to HIGH  ON 6
        delayzz();
        delayzz();
        delayzz();
        delayzz(); 
        delayzz();
        delayzz();
        LATDbits.LATD4 = 0;   // RD-4 to LOW   OFF 5
        delayzz();
        delayzz();
        LATDbits.LATD5 = 0;   // RD-5 to LOW    OFF 6
        delayzz();
        delayzz();
        LATDbits.LATD6 = 0;    // RD-6 to LOW   OFF 7
        delayzz();
        delayzz();
        LATDbits.LATD7 = 0;    // RD-7 to LOW   OFF 6
        delayzz();
        delayzz();
        delayzz();
        delayzz(); 
        delayzz();
        delayzz();
        delayzz();
        delayzz(); 
        delayzz();
        delayzz();
 
        //LATDbits.LATD6 = 0;    // RD-4 to LOW OFF5
 
//      LATDbits.LATD7 = 0;    // RD-5 to LOW   OFF6
        
//  delayzz();
 
 
         }
}


Thank you in advance
David :-D
 
Last edited by a moderator:

Hi,

The power on default for those ports that have an Analogue function is Analogue Input.

To use them as Digital Output you must first change them from Analogue to Digital by means of the ANSEL instruction ( ANSEL applies to your chip, not all Pics use that register )

Once digital you then set them to Output by means of the TRISA instruction.

Have a look at these tutorial which should help with your C and other Pic things generally

Though the datasheet for that chip shows most examples in assembly code its still a mine of information, though it can seem difficult to follow for the beginner.


**broken link removed**

http://www.electro-tech-online.com/articles/nigel-goodwins-tutorials-in-c.467/
http://www.winpicprog.co.uk/pic_tutorial.htm
http://www.amqrp.org/elmer160/lessons/
http://www.epemag.wimborne.co.uk/resources.htm
http://www.mikroe.com/chapters/view/5/chapter-4-timers/
 

Wow!! Thanks for the reply, I'm at my wits end. I've been on the web all day and have tried the ANSEL register. Probably incorrectly but commented out those lines in code.

I will look at those sites, Thanks. I have years ago written in assembly, for windows but havn't gotten around to it with PIC yet. The data sheet is still confusing, this register depends on that register, etc. Still want to be careful when changing values of registers, unless I know what I am doing. (I don't)

But, Thanks for the reply and suggestions.
David
 

Wow!! Thanks for the reply, I'm at my wits end. I've been on the web all day and have tried the ANSEL register. Probably incorrectly but commented out those lines in code.

I will look at those sites, Thanks. I have years ago written in assembly, for windows but havn't gotten around to it with PIC yet. The data sheet is still confusing, this register depends on that register, etc. Still want to be careful when changing values of registers, unless I know what I am doing. (I don't)

But, Thanks for the reply and suggestions.
David



Hi,

Think your code is almost correct.

I use the 18F4520 but with Assembly, think its almost the same as your K chip.

PBADEN = OFF means that portB will be Digital

ANSEL = 0b11100000 means PORTA 0,1,2,3 and 5 will become Digital RA4 is not Analogue

TRISA = 0b11000000 means PORTA 0 -6 become outputs.

Let us know if that works...

Though you are only testing, portA is generally used for Inputs

That Gooligum link, is both Assembler and C if you look further down that page and also go to its home page for the rest of the FREE C tutorials.
 

Hi!

Thanks for the info an reply.

I made the changes, same problem. Meanwhile I have been looking in all my books for info. We'll figure it out.

I was wondering how to change the ANSEL bits and you provided that solution. Much thanks

I'll def. reply with my results so we can all learn.

David
 

Hi,

Afraid I cannot help with C code, but have just run this assembly code in a simulator and it seems to work ok.

The source and .hex files are attached so you can try it yourself without having to compile the code, so you can prove your hardware is ok.
 

Attachments

  • 000028.jpg
    000028.jpg
    72.3 KB · Views: 85
  • flash.zip
    1.2 KB · Views: 82

Hi! I have used PIC18F45K22, so I believe it would be similar to PIC18F45K20. Unlike most PICs there is no SFR called ANSEL or ANSELH instead it has ANSELA, ANSELB, ANSELC, ...... By default there are set to analog. So you need to clear the register to make it digital. What you need to do is ANSELA = 0 or ANSELD = 0. I don't know why you have first set the TRIS to input and then cleared to make it output after a delay. I think it is not necessary, just make the TRIS register to 0. Hope this helps.
 

Read this pdf to make delay functions **broken link removed**

Configure ANSELx registers as mentioned. Maybe you also need to dsable Comparators and Comparator Reference Voltage using CMCON and CRVCON.
 

Incredible, Works fine with assembly. Thanks so much for your work. Everyone has been so kind on this forum.

I did a-lot of reading yesterday and last night, including data sheet :grin:, and all deserve a reply for their help.
But for now, I'll go make a pot of coffee and rethink everything. User suggestions, web search, and personal search's.
This is not to suggest anyone who has a thought on this, shouldn't reply. Please if anyone is willing make a suggestion.
Much thanks for you code, works. Now on to C.
 

Ok, I loaded the PGM given by wp100.

He's right, HEX works fine. (It looked so nice blinking)

I spent the next two days studying "inline assembly" with the hope I could just add the assembly and go on. (with permission from wp100, of course)

No such luck:-(

So Friday, I went back to C, in what should have worked.

Added the Port lines TRISA = 0b00000000; // Same for TRISB,C.

Tested with LATAbits.LATA0 = 1; and so on for the other ports.

Works GREAT!!!!! :p :p

So, got to mark this one solved. Code in C as usual. I can only guess wp100 code cleared something, don't know what happen'd for sure.

Thanks to everyone who responded. Learning is work.
 

Ok, I loaded the PGM given by wp100.

He's right, HEX works fine. (It looked so nice blinking)

I spent the next two days studying "inline assembly" with the hope I could just add the assembly and go on. (with permission from wp100, of course)

No such luck:-(

So Friday, I went back to C, in what should have worked.

Added the Port lines TRISA = 0b00000000; // Same for TRISB,C.

Tested with LATAbits.LATA0 = 1; and so on for the other ports.

Works GREAT!!!!! :p :p

So, got to mark this one solved. Code in C as usual. I can only guess wp100 code cleared something, don't know what happen'd for sure.

Thanks to everyone who responded. Learning is work.



Hi,

Glad you have got it going, :grin: though cannot say why, but sure the others can help on the C.

My Assembly example should have specified LAT instead of PORT as stated by Microchip, but old habits die hard, its basically to avoid possible problems writing /reading to a Port.

For details of it just google PORT v LAT


Have a look at those tutorials mentioned earlier to help get you into things more, have you a particular project you are aiming for ?

Always help here if you get stuck.
 
  • Like
Reactions: Hawk44

    Hawk44

    Points: 2
    Helpful Answer Positive Rating
Hi,

My first project was just to control some LED's. Actually the 5050 tri-color led, which is one reason I needed all the ports.
Personally, I just want to learn everything I can. I have got board etching down pat, now working on the programming.
I'll attach a photo, you might as well see the fruit of your labor. Shows the board on the right with SMD resistors setting the LED up to run on 3 volts. (running the 18F45K20 at 3 volts.) The 18F45K20 is on the board to the left. The right board is plugged in to a breadboard. So now I play with code. Got tons of projects in mind, all learning......Probably will need help....Thanks
Thanks for all your help, and kindness.Photo1.jpg.JPG
David
 

Hi,

Looking good :razz:


Just a couple of things on the current consumption, I only have the PK2 but from what I read they say the PK3 can only supply 30ma to the target board.
I cannot see any wires running to an external psu so you want to be careful you do not fry the pk3 with those all the leds on.

Also the 45k20 can handle 25ma per output pin, but you cannot run a led at 25ma off each pin because the whole chip can only handle up to 200ma.

Just that in either case if you have an accidental short on the outputs / led it could be costly.

Typically the way to overcome that is to use a small wall power pack to supply the heavy current for the leds and use a transistor driver or much easier a ULN2803A octal driver between the pic pins and the leds.

What you can do programming wise is use the PWM modules to drive each of the leds RGB segments and create your own light show.

Enjoy..
 

Woops!!!!!!!! Thanks for the info....I mean REALLY Thanks.
Guess we can say the PK3 can do more than 30 ma. Well more than 30ma. I might say. Wouldn't want to burn it up tho.....so hard to explain why I have only used it a few days and sent it up in smoke. I wasn't running the LED's them hard .
I knew that each pin was 25ma. but I did not know the whole chip limit of 200 ma. Very good to know. Design change.... coming ....I was getting ready to move to batteries but that was mostly luck I was moving in that direction as far as timing goes. Wow, so good to know. I'll look up the ULN2803A and go from there.
Thanks again man, you've been a great help.
 

Woops!!!!!!!! Thanks for the info....I mean REALLY Thanks.
Guess we can say the PK3 can do more than 30 ma. Well more than 30ma. I might say. Wouldn't want to burn it up tho.....so hard to explain why I have only used it a few days and sent it up in smoke. I wasn't running the LED's them hard .
I knew that each pin was 25ma. but I did not know the whole chip limit of 200 ma. Very good to know. Design change.... coming ....I was getting ready to move to batteries but that was mostly luck I was moving in that direction as far as timing goes. Wow, so good to know. I'll look up the ULN2803A and go from there.
Thanks again man, you've been a great help.

Hi,

Have a look at this page, it explains the basic input and outputs for heavier currents.
http://www.winpicprog.co.uk/pic_tutorial_extras.htm

A ULN2803A basically 8 separate transistors in one package that also includes the input resistor and output diodes for when driving relays, it save a lot of separate parts and wiring.
You can drive it from various voltages up to 50v and a realistic maximum current of 500ma though the whole chip.

This diagram shows a typical led driver, but note that the resistors with be dependant on the + voltage you drive then with.
When using leds you do not need to connect pin 10 to the + voltage.
Your pics 0v and the battery 0v must be connected together.


If you look in the 45k20 datasheet, first page of the Electrical specifications section it shows the max currents under various conditions.
 

Attachments

  • 2803led.jpg
    2803led.jpg
    16.4 KB · Views: 76

Hi,

Have a look at this page, it explains the basic input and outputs for heavier currents.
http://www.winpicprog.co.uk/pic_tutorial_extras.htm

A ULN2803A basically 8 separate transistors in one package that also includes the input resistor and output diodes for when driving relays, it save a lot of separate parts and wiring.
You can drive it from various voltages up to 50v and a realistic maximum current of 500ma though the whole chip.

This diagram shows a typical led driver, but note that the resistors with be dependant on the + voltage you drive then with.
When using leds you do not need to connect pin 10 to the + voltage.
Your pics 0v and the battery 0v must be connected together.


If you look in the 45k20 datasheet, first page of the Electrical specifications section it shows the max currents under various conditions.


Yes, I had ordered some transistors to handle additional current but what I totally missed was the total current limit of the micro. Just forgot to consider it, actually assumed it would be ok. I know, that's bad when designing things thanks for the reminder. I was online a while ago, after looking at the ULN2803A data, and ordered some. Still have to design the final PCB, still learning, and no smoke yet....:smile:...so all is good.

Thanks again man, you have helped SO much.

Have a good weekend, I've got some reading to do. :grin:
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top