PORTA RA0. Help Needed. thank

Thread Starter

Ralf

Joined Mar 16, 2009
31
i using PIC18f24k20 Microchip

i going to send a number intro that RA0
may i know how to write the code?

i using MPLAB C compiler.

thank for the help..
 

dig1

Joined Jul 31, 2008
18
if you mean a bit, that's possible i don't know about a number that's a whole set of bits together.... if you are wanting to populate the all the signals on portA, just movlw (number) then movwf PORTA should do...there are other ways to do it too but this most probably is the easiest.. if you just want to change the number then that's a whole different story... (this is based on my knowledge of doing stuff on the PIC16866) so it might be different for your version
 

thatoneguy

Joined Feb 19, 2009
6,359
In C, PORTA=1 will set RA0 to 1, if TRISA is 0, with comparators and ADC turned off for that port.

Is that the processor that comes on the 44 pin PIC Demo board?
 

dig1

Joined Jul 31, 2008
18
boy i don't mean to hijack the OPs thread, but PORTA=1 will set RA0 to 1 or RA0 through RA5 to 1? i ask this because i am pretty new to PICs
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
thank guy for the reply..

thatoneguy: i using PIC18f24k20 microchip.. is not the demo board that 44 pin chip. i guess i cannot send number to port A because it only read 1 and 0.. i have another question that is about ADC..

i know that ADRESH this register is to store the ADC result. if i wanna to convert the result to 3 7-segment, how do i do it??

probably i wish to know is the result in ADRESH i get is binary , Hex or decimal ?

thank guy once again for helping me so much
 

thatoneguy

Joined Feb 19, 2009
6,359
Here is ADC Code with Engrish comments that I found on my drive:
This writes to 7 segment, characters are defined in TABLE for which digits are lit.


Rich (BB code):
1: #include[SIZE=+1]<pic[SIZE=+1].[/SIZE]h[SIZE=+1]>[/SIZE]              //include MCU head file
 2:  __CONFIG[SIZE=+1]([/SIZE]0x1832[SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]        
 3: //THE configure of MCU,watchdog OFF,electrify delay OPEN,power down check OFF,
 4: //LOW power programme OFF,encrypt,4M crystal HS surge.
 5:  const char TABLE[SIZE=+1][[/SIZE][SIZE=+1]][/SIZE][SIZE=+1]=[/SIZE][SIZE=+1]{[/SIZE]0xc0[SIZE=+1],[/SIZE]0xf9[SIZE=+1],[/SIZE]0xa4[SIZE=+1],[/SIZE]0xb0[SIZE=+1],[/SIZE]0x99[SIZE=+1],[/SIZE]0x92[SIZE=+1],[/SIZE]0X82[SIZE=+1],[/SIZE]0XF8[SIZE=+1],[/SIZE]0X80[SIZE=+1],[/SIZE]0X90[SIZE=+1]}[/SIZE][SIZE=+1];[/SIZE] 
 6:  //define the table of constant 0-9
 7:  void  DELAY[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]              //delay function declare
 8:  void  init[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]               //I/O PORT initialize function declare
 9:  void  display[SIZE=+1]([/SIZE]int x[SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]       //display function declare
10: //------------------------------------------------
11:   //main program
12:  void  main[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE]               
13:  [SIZE=+1]{[/SIZE]
14:    int result[SIZE=+1]=[/SIZE]0x00[SIZE=+1];[/SIZE]         //define convert result register               
15:    while[SIZE=+1]([/SIZE]1[SIZE=+1])[/SIZE]                 //dead circle                                  
16:    [SIZE=+1]{[/SIZE]                                                                       
17:    int i[SIZE=+1];[/SIZE]                   //define the times of circle control           
18:    result[SIZE=+1]=[/SIZE]0x00[SIZE=+1];[/SIZE]             //clear the convert result                     
19:    for[SIZE=+1]([/SIZE]i[SIZE=+1]=[/SIZE]5[SIZE=+1];[/SIZE]i[SIZE=+1]>[/SIZE]0[SIZE=+1];[/SIZE]i[SIZE=+1][SIZE=+1]-[/SIZE][SIZE=+1]-[/SIZE][/SIZE][SIZE=+1])[/SIZE]         //get the average of five convert results      
20:      [SIZE=+1]{[/SIZE]                                                                     
21:       init[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]               //call initialize function                     
22:       ADGO[SIZE=+1]=[/SIZE]0X1[SIZE=+1];[/SIZE]             //start convert                                
23:       while[SIZE=+1]([/SIZE]ADGO[SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]          //wait for finish convert                      
24:       result[SIZE=+1]=[/SIZE]result[SIZE=+1]+[/SIZE]ADRESL[SIZE=+1];[/SIZE] //accumulative total convert results           
25:       [SIZE=+1]}[/SIZE]                                                                    
26:       result[SIZE=+1]=[/SIZE]result[SIZE=+1]/[/SIZE]5[SIZE=+1];[/SIZE]      //get the average of five convert results      
27:      display[SIZE=+1]([/SIZE]result[SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]       //call display function                        
28:    [SIZE=+1]}[/SIZE]
29:   [SIZE=+1]}[/SIZE]
30: //-----------------------------------------------
31:  //initialize function
32: 
33:  void  init[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE]               
34:   [SIZE=+1]{[/SIZE]
35:    PORTA[SIZE=+1]=[/SIZE]0XFF[SIZE=+1];[/SIZE]               
36:    PORTD[SIZE=+1]=[/SIZE]0XFF[SIZE=+1];[/SIZE]               //close all display                                           
37:    TRISA[SIZE=+1]=[/SIZE]0X1[SIZE=+1];[/SIZE]                //set RA0 INPUT,the others OUTPUT                             
38:    TRISD[SIZE=+1]=[/SIZE]0X00[SIZE=+1];[/SIZE]               //set D PORT all OUTPUT                                       
39:    ADCON1[SIZE=+1]=[/SIZE]0X8E[SIZE=+1];[/SIZE]              //set RA0 simulate INPUT,the others general I/O               
40:    ADCON0[SIZE=+1]=[/SIZE]0X41[SIZE=+1];[/SIZE]              //system clock Fosc/8,select RA0 routeway,allow ADC work      
41:    DELAY[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]                  //ensure sampling delay                                       
42:    [SIZE=+1]}[/SIZE]
43: //-----------------------------------------------
44: //display function            
[SIZE=+1][/SIZE]

[/SIZE]


CONTINUED IN NEXT POST
[SIZE=+1][/SIZE]Code coloriaztion by http://www.chamisplace.com/colorizer/cc.asp
 

thatoneguy

Joined Feb 19, 2009
6,359

ADC - 7 Segment - continued.....

Rich (BB code):
45:  void display[SIZE=+1](int x[SIZE=+1])[/SIZE]          
46:    [SIZE=+1]{[/SIZE]
47:      int  bai[SIZE=+1],[/SIZE]shi[SIZE=+1],[/SIZE]ge[SIZE=+1],[/SIZE]temp[SIZE=+1];[/SIZE]   //define four temporary variable                 
48:      temp[SIZE=+1]=[/SIZE]x[SIZE=+1];[/SIZE]                 //temporary keep AD convert result               
49:      bai[SIZE=+1]=[/SIZE]temp[SIZE=+1]/[/SIZE]0x64[SIZE=+1];[/SIZE]          //get display hundred bit                        
50:      shi[SIZE=+1]=[/SIZE][SIZE=+1]([/SIZE]temp[SIZE=+1]%[/SIZE]0x64[SIZE=+1])[/SIZE][SIZE=+1]/[/SIZE]0xa[SIZE=+1];[/SIZE]    //get display ten bit                            
51:      ge[SIZE=+1]=[/SIZE][SIZE=+1]([/SIZE]temp[SIZE=+1]%[/SIZE]0x64[SIZE=+1])[/SIZE][SIZE=+1]%[/SIZE]0xa[SIZE=+1];[/SIZE]     //get display Entries bit                        
52:      PORTD[SIZE=+1]=[/SIZE]TABLE[SIZE=+1][[/SIZE]bai[SIZE=+1]][/SIZE][SIZE=+1];[/SIZE]       //get the display hundred bit code from table    
53:      PORTA[SIZE=+1]=[/SIZE]0x37[SIZE=+1];[/SIZE]             //RA3 OUTPUT low,light hundred bit display       
54:      DELAY[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]                //delay some time,ensure display brightness      
55:      PORTD[SIZE=+1]=[/SIZE]TABLE[SIZE=+1][[/SIZE]shi[SIZE=+1]][/SIZE][SIZE=+1];[/SIZE]       //get the display ten bit code from table        
56:      PORTA[SIZE=+1]=[/SIZE]0x2F[SIZE=+1];[/SIZE]             //RA4 OUTPUT low,light ten bit display           
57:      DELAY[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]                //delay some time,ensure display brightness      
58:      PORTD[SIZE=+1]=[/SIZE]TABLE[SIZE=+1][[/SIZE]ge[SIZE=+1]][/SIZE][SIZE=+1];[/SIZE]        //get the display Entries bit code from table    
59:      PORTA[SIZE=+1]=[/SIZE]0x1F[SIZE=+1];[/SIZE]             //RA5 OUTPUT low,light Entries bit display       
60:      DELAY[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]                //delay some time,ensure display brightness      
61:    [SIZE=+1]}[/SIZE]
62: 
63: //----------------------------------------------
64: //delay program 
65: void  DELAY[SIZE=+1]([/SIZE][SIZE=+1])[/SIZE]              //delay program            
66:     [SIZE=+1]{[/SIZE]                                                 
67:      int i[SIZE=+1];[/SIZE]                 //define integer variable 
68:      for[SIZE=+1]([/SIZE]i[SIZE=+1]=[/SIZE]0x100[SIZE=+1];[/SIZE]i[SIZE=+1][SIZE=+1]-[/SIZE][SIZE=+1]-[/SIZE][/SIZE][SIZE=+1];[/SIZE][SIZE=+1])[/SIZE][SIZE=+1];[/SIZE]     //delay                   
69:     [SIZE=+1]}
[/SIZE][/SIZE]
Code coloriaztion by http://www.chamisplace.com/colorizer/cc.asp
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
thank thatoneguy.

i copy my code here.. i think there and i got a few question to ask.

#include <p18f24k20.h>
#include <adc.h>

#pragma config FOSC = XT //Oscillator Setting
#pragma config WDTEN = OFF //Turn off WatchDog


void ADC_Init (void)
{
ADCON0 = 0x01; // select channel 0 (AN0) to read the potentionmeter voltage and thurn on ADC
ADCON1 = 0x00; // sets bits VCFG1 and VCFG0 in ADCON1 so the ADC coltage reference is VSS to VDD
ADCON2 = 0x01; // A/D conversion clock select bits to Fosc/8
}

void ADC_Convert (void)
{
int temp;
ADCON0bits.GO_DONE = 1; //Start Conversion
while (ADCON0bits.GO_DONE == 1); // wait for it to complete
ADRESH = temp ; // return high byte of result
}

void main (void) //Function
{

TRISA = 0x01; //PORT A RA0 is Input and the rest output
TRISB = 0; //PORT
TRISC = 0;



}

may i know is there any wrong with my program?
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
PORTA[SIZE=+1]=TABLE[SIZE=+1][[/SIZE]bai[SIZE=+1]][/SIZE][SIZE=+1];[/SIZE] //get the display hundred bit code from table
[/SIZE] PORTB[SIZE=+1]=TABLE[SIZE=+1][[/SIZE]shi[SIZE=+1]][/SIZE][SIZE=+1];[/SIZE] //get the display hundred bit code from table
[/SIZE] PORTC[SIZE=+1]=TABLE[SIZE=+1][[/SIZE]ge[SIZE=+1]][/SIZE][SIZE=+1];[/SIZE] //get the display hundred bit code from table [/SIZE]

thatoneguy i got something to ask u.. if i change it like this will my display go to port A B and C?
bcoz my Port A , B and C each have 1 7-segment display
 

thatoneguy

Joined Feb 19, 2009
6,359
No, the LEDs are multiplexed so the data is on the same bus, and the common anode/cathode is switched to make that segment light up. They are scanned hundreds of times per second, so it looks persistent.

Easier to program, takes fewer uC pins, and is the standard for 7 segment displays, especially when they are over 2 digits.
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
cool.. i about to off work now.. tomorrow will u be on? just in case i have a few question.. and hope u can reply.. thank alot thatoneguy.

thank everyone for helping me..
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
void DELAY() //delay program
{
int i; //define integer variable
for(i=0x100;i--;); //delay
}

may i know if i want a 1 sec delay.. wad number should i use?
 

thatoneguy

Joined Feb 19, 2009
6,359
There should be a delay_ms(), delay_cycle() or similar function in Microchip C18, the above is written to be "translate to assembly" friendly.
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
when i build my program i have a error on this line..

void display(int x)

the error is syntax error

why?? need help again.. thank
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
just wanted to know. my program is almost done and i wish to add on an interrupt for my program. just wish to know how the interrupt really work? how should i program it?
 

thatoneguy

Joined Feb 19, 2009
6,359
The error with the display function most likely has something to do with the following.

You need a function prototype for each function, stating what values are passed to the function and how, along with what type of value is returned from the function (void = nothing in this case). This make things easier/faster for the compiler to find errors in fewer passes, as well as an index of functions for the program author to refer to.

at the top of the code, you will see:

void display (int x);

then the rest of the code

Then the actual function:
void display (int x)
{

..
}

Some compilers do not like a named parameter in the prototype, so removing the variable name 'x' at the top of the program, if that is where the flag is coming up, should fix it.
try:
void display (int);


Using the C Library delay_ms() functions, the headache of calculating how many cycles to 'waste' with various clock speeds is removed, as long as the #pragma clock 16000000 is in the program (for a 16Mhz processsor).


As far as using seperate 7 segment displays on seperate ports, that could be done, but the multiplexing code would need to be removed. If you already have it wired one way, it can be made to work, the overall concern is total power dissipation of the package. With 3 displays, nearly all I/O ports would be tied up for the display only.

The 18F Series allows for 25mA source/sink per pin, but total package dissipation is a max of 200mA or 1W, whichever is lower. If you don't mind using 21 pins for display, removing the mplex software (what the delay is for), and the total draw for all LED's display "8" is under 200mA, you can use them on seperate ports.

Speaking of I/O pins and power on them, is there a reason you are using an external clock for this application? The internal oscillator runs at 16Mhz, and up to 64Mhz with a bit of PLL programming (shown in datasheet).

Lastly, what did you want to implement interrupts to do?

Once it is working that far, compiles, and can display, we will tune up the ADC and Interrrupts. I have a 18f45k20, and an order of 18f26k20 inbound, so i'll be able to give the exact code and diagram if all else fails. :) I normally use BoostC from http://www.sourceboost.com, The license for non-commercial use is Extremely Affordable, and it is 'free' for smaller programs. License also includes their NovoRTOS operating system to help with multitasking. Quite a bargain for when your C18 optimizer expires.

*I am not affiliated with sourceboost at all, just an extremely happy customer of theirs*

--ETA: Sorry for the late reply! I didn't notice this thread got updated until just now...
 
Last edited:

Thread Starter

Ralf

Joined Mar 16, 2009
31
thank thatoneguy.. u help me alot.. :)

hope i did not waste your time.. u really help me alot for my project. :)

currently i using switch case for my program because i found out that my IC chip PORTA not all pin is connected to LED A,

current the problem i facing is writing switch case and as i post here b4 that i using 3 LED to display my result and i found out that:

LED A using PORT of RA1,RA2,RA3,RA4,RA5,RB6 and RB7 (Using PORTA and B)
LED B using PORT of RB0,RB1,RB2,RB3,RB4,RB5 and RC7 (Using PORTB and C)
LED C using PORT of RC0,RC1,RC2,RC3,RC4,RC5 and RC6 (Only PORTC)

as can see that i using 2 port for 1 LED that why i planning to using switch case.

thatoneguy can i ask u that for 18F24k20 if i wanna to set porta RA1 as a 0 what command should i write? i know is something LATAbits.___ = 0
i dont know what to put at the place i underline.

once again.. really thank for the help.. i super appreciate it.. thank bro

PS: if anything dont really understand please let me know.. i try to phrase it other way.. thank!
 

Thread Starter

Ralf

Joined Mar 16, 2009
31
i think i got it how to write a switch case but now is how to write that LATAbits.___ = 0 this part. once i got it i think i can finish my switch case stuff..

thank for the help.. :)
 
Top