The Godfather talking
You can run, but you can't hide.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 09, 2016, 10:07:26 22:07


Login with username, password and session length


Pages: [1]
Print
Author Topic: Proper Way to Initialize PIC18?  (Read 5116 times)
0 Members and 1 Guest are viewing this topic.
solarwind
Newbie
*
Offline Offline

Posts: 32

Thank You
-Given: 4
-Receive: 1


« on: January 10, 2009, 06:33:36 18:33 »

Hey all, I normally use the PIC16 series but I decided to dive into the PIC18 series. I have a PIC18F2620. I was unable to find any recent code on how to properly initialize this device (configuration words and stuff like that). If anyone has any C code or ASM code (C code preferred) for this device or any other related 18F device, please post.
Logged
pisi
Newbie
*
Offline Offline

Posts: 14

Thank You
-Given: 4
-Receive: 2


pisi


« Reply #1 on: January 10, 2009, 07:22:35 19:22 »

hello
I was writing my codes in assembler 5 years ago
and I decided to switch 18 series like you
when I saw the instruction set and compared with 16 series' I had decided to learn and write my codes in c
and I had tried to use hitech and ccs and decide to use ccs c compiler
I can offer to use and learn ccs
it has got a wizard to initialize any controller easily
you may learn and improve your codes so fast
but if you want to spent time I offer to learn Microchip c
anyway it's up to you to select the compiler
you may read something related with comparing compilers
but you will need in any case to switch to c compiler not continue with assembler
Logged

pisi
icecubetray
Newbie
*
Offline Offline

Posts: 14

Thank You
-Given: 3
-Receive: 0


« Reply #2 on: January 11, 2009, 05:45:32 05:45 »

Ultimately it depends on the compiler you use/choose and your application.

Here is a sample for the ccs compiler.  This chip has all kinds of functions......and it depends what you are trying to do and what you need.

I agree with pisi the ccsc project wizard does a nice job generating this for you....as I have shown here as an example


MAIN.H
Quote
#include <18F2620.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC                    //Internal RC Osc
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES XINST                    //Extended set extension and Indexed Addressing mode enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)

MAIN.C
Quote
#include "C:\main.h"


#define CAN_USE_EXTENDED_ID FALSE
#include <can-18xxx8.c>

void main()
{
   can_init();
   


   setup_adc_ports(ALL_ANALOG|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

do
   {
   // TODO: USER CODE!!
}while(true);
}


good luck with your project!

Cheers
Logged
pisi
Newbie
*
Offline Offline

Posts: 14

Thank You
-Given: 4
-Receive: 2


pisi


« Reply #3 on: January 11, 2009, 12:38:56 12:38 »

yes it is good job iceqube Smiley
and for additiyon you add a line to do-while loop for port b
do
{
output_b(0xff);
delay_ms(100);
output_b(0x00);
delay_ms(100);
} while(true);

for addition
as usually i'm using ports' direct memmory adress for not changing everytime tris registers
for example i define portb as
#byte PortB = 0xf81
and
add to init routine
set_tris_b(0x00);
and send PortB=0xff
it is making data loading port so fast or opposite for input also
if you define set_tris_b(0b00001111)  half of port is output half of input
this bit defined written is not avaliable in any compiler (IAR HITECH or MICROCHIP)
but for example if you want to test with a bit
you may define
#bit LED = 0xf81.0// it must be defined before than program ofcourse
and in init routine if you add set_tris_b(0x00);
after init routine
you may write test code as
while(1)
{LED++; delay_ms(100); }
that's all Smiley
and compiler selects correct instruction for the chip series
for example for 16 series it is changing led bit value with ex-or instruction
but for 18 series it is changing led value by toggle instruction
it means that compiler likes to write codes shortly Smiley

Logged

pisi
caveman508
Junior Member
**
Offline Offline

Posts: 41

Thank You
-Given: 12
-Receive: 6

READ, REAd, REad, Read


« Reply #4 on: January 12, 2009, 12:22:55 00:22 »

The default values for the chip is explained in the data sheet.  Most defaults make sense, but they usually make Port A analog by default.  As IceCubeTray pointed out, the #include <18F2620.h> line is definition file for the device you are going to use, it is a bit hard to read, but when reading the .h file along with the data sheet, you will see it make more sense.
Logged
sittichoke
Newbie
*
Offline Offline

Posts: 7

Thank You
-Given: 8
-Receive: 2


« Reply #5 on: January 12, 2009, 04:45:06 04:45 »

I'm also agree with pisi to use ccs c.
Cause it has wizard and tools that you can initialize properly and clear understanding on all hardware of microchip.
and It has more example on most of function. like adc, pwm, rtos etc.
but you still need to read datasheet of mcu that you use to see some specific feature.
 Grin
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC