PIC18F4550 Analog port control

General discussion on mikroC.
Post Reply
Author
Message
bahatay
Posts: 14
Joined: 05 May 2011 13:25

PIC18F4550 Analog port control

#1 Post by bahatay » 16 Jun 2011 13:10

Hi, I am using PIC18F4550, LCD, usb port and only one analog input. The problem is I am having problem with how to control analog ports, also digital ports. I need 1 analog input port and about 24 digital outputs.
I designed a PCB LCD using RA0,1,3,4,5, RE0 ports but I was not able to control RA0 & RA1 ports. So I changed soldering and replaced them with RE1,2. Thus I was able to run LCD with PIC16F887. However I was not able to run LCD with 18F4550. I tried lots of things including setting all ports to digital, resoldering port RE2 to RC0. I am turning on-off-on-off so on to test which ports I am able to control.

Here is the code of MicroC


void main(){

// CMCON = 0x07;
// ADRESL = 0b10000000;
// ADRESH = 0b10000000;
// ADCON0 = 0b00000000; //AN0 as analog input but ADC module is disabled
// ADCON1 = 0b00001110; // AN0 port is selected as anolog input
ADCON1 = 0x0F; //All for digital operation it says

TRISA = 0;
TRISE = 0;
TRISC = 0;

while(1){
TRISA0_bit=1;
TRISA1_bit=1;
TRISA2_bit=1;
TRISA3_bit=1;
TRISA4_bit=1;
TRISA5_bit=1;
TRISE0_bit=1;
TRISE1_bit=1;
TRISE2_bit=1;
TRISC0_bit=1;
TRISC1_bit=1;
TRISC2_bit=1;
Delay_ms(200);

TRISA0_bit=0;
TRISA1_bit=0;
TRISA2_bit=0;
TRISA3_bit=0;
TRISA4_bit=0;
TRISA5_bit=0;
TRISE0_bit=0;
TRISE1_bit=0;
TRISE2_bit=0;
TRISC0_bit=0;
TRISC1_bit=0;
TRISC2_bit=0;

Delay_ms(200);
}

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: PIC18F4550 Analog port control

#2 Post by slavisa.zlatanovic » 17 Jun 2011 09:06

Hi!

We strongly encourage users to switch to PRO version because non-PRO version is an ancestor of PRO compiler, which is no longer being developed. Every new feature, improvement and bug fix will only affect new versions of PRO compilers. Therefore, even if you have come across some compiler bug in older compilers, we won't be able to fix it, nor help you, because that's just natural order of things.

P.S.
If you want to set all pins as digital I/Os then you'll need to write:

Code: Select all

 ADCON1 = 15;
 CMCON = 7;
Best regards
Slavisa

bahatay
Posts: 14
Joined: 05 May 2011 13:25

Re: PIC18F4550 Analog port control

#3 Post by bahatay » 17 Jun 2011 14:35

I am using 2009 pro version but I was not able to licence it. I bought EasyPIC6, with version 8.0 but because of my vendor I could not get a licence file request.

Thanks to your code it just works fine, for only AN0 pin to be anlog port I should write ADCON1 = 14, shoudn't I?

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: PIC18F4550 Analog port control

#4 Post by slavisa.zlatanovic » 20 Jun 2011 09:01

Hi!

Yes, you're right.

Code: Select all

ADCON1 = 0x0E;
P.S.
I am using 2009 pro version but I was not able to licence it.
Just repeat the registration procedure from your PRO compiler version. If you're a registered user
you'll receive new license key free of charge.
Best regards
Slavisa

bahatay
Posts: 14
Joined: 05 May 2011 13:25

Re: PIC18F4550 Analog port control

#5 Post by bahatay » 20 Jun 2011 14:31

I am not a registered user. I am trying to fill in this form (http://www.mikroe.com/eng/pages/activat ... 584FFD/17/) but I dont have the invoice nor the 2CO number. Besides I dont know which distributor its bought from. I bought this product from the importer company and couldnt learn these information yet.


Now I am able to control all the analog ports as it should be, but I am not able run 2x16LCD properly with 18f4550. It shows nothing before reset, and after reset its quite meaningless, see image below. It was very easy with 16f887. Here is my code


sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

char *text;


void main(){
TRISB = 0;

ADCON1 = 14;
CMCON = 7;
// ADCON1 = 0b00001111; // Configure AN0 as analog input and other pins as digital
// CMCON = 0b00000011; //All for digital operation it says


text = "MicroElektronika";
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

while(1){

LCD_Out(1,1,text);
Delay_ms(1000);
}
}


Image:
http://www.imageupload.org/?d=0846313C1

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: PIC18F4550 Analog port control

#6 Post by slavisa.zlatanovic » 21 Jun 2011 08:38

Hi!

Instead of:

Code: Select all

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
You should write:

Code: Select all

sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
P.S.
Write me an email and provide all information that could help me find you in our customer data base.
slavisa.zlatanovic@mikroe.com
Best regards
Slavisa

ccfred
Posts: 4
Joined: 05 Mar 2011 22:39
Location: Québec, Canada

Re: PIC18F4550 Analog port control

#7 Post by ccfred » 23 Jun 2011 22:19

I got the same type of bad LCD behavior with my pic18f4431. I try a lot of things but some unkown caracters appear.

Here my code

// LCD module connections
// Lcd pinout settings
//sbit LCD_RS at RB4_bit;
//sbit LCD_EN at RB5_bit;
//sbit LCD_D7 at RB3_bit;
//sbit LCD_D6 at RB2_bit;
//sbit LCD_D5 at RB1_bit;
//sbit LCD_D4 at RB0_bit;
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;

// CONFIG QEI ERR BIT NOT PRESENT IN P18F4431.C
const register unsigned short int QERR = 6;
sbit QERR_BIT at QEICON.B6;

// LOCAL var declared
char txt_cnt[12];
char txt_poss[7];
char txt_dir[3];
char txt_turn[7];
char txt_err[4];
char txt_speed[7];
char txt_long[12];
char err = 0;

int temp = 0;
int encoderTurnPulseX4 = 7999;
int scan = 0;
int speed = 0;
int i1 = 0;
int i2 = 0;
int i3 = 0;
unsigned int actual_pulse_position;

long turncnt = 0;
long ab = 0;
long lastab = 0;
long diffab = 0;
long l1 = 123456789;
long l2 = 0;
long l3 = 0;

sbit zz1 at ra2_bit;
sbit aa1 at ra3_bit;
sbit bb1 at ra4_bit;
sbit direction at UP_DOWN_bit;

void Init(){
ANSEL0 =0; // DÉACTIVE ENTRÉES ANALOGUE
ANSEL1 =0; // DÉACTIVE ENTRÉES ANALOGUE
ADCON0 = 0;
PORTA = 0; // RESET PORTA
TRISA =0X3F; // ACTIVE LES 6 ENTRÉES PORTA
INTCON = 0b10000000;
CAP1CON = 0;
CAP2CON = 0;
CAP3CON = 0;
QEICON = 0b10011010; // 4x w/o index qeicon = 0b10011010; or 2x w index qeicon = 0b10000100;
MAXCNTH = 0x1f; // -1+(2000*4)=7999=0x1f3f
MAXCNTL = 0x3f; // -1+(2000*2)=3999=0x0f9f

IPEN_BIT=0; // IPEN: Interrupt Priority Enable bit
// 1 = Enable priority levels on interrupts
IC2QEIE_BIT=1; // IC2 Interrupt Flag/QEI Interrupt Flag Enable bit
// 1 = IC2 interrupt enabled)
PEIE_BIT=1; // Peripheral Interrupt Enable bit
// 1 = Enables all unmasked peripheral interrupts
GIE_BIT=1; // Global Interrupt Enable bit
// 1 = Enables all unmasked interrupts
// do nothing portb is asigned to LCD
PORTB=0X00; // reset PORTB FOR LCD. DEFINE UP PORTC =0X00;
// RESET PORTC
TRISC =0X00; // ACTIVE LES 8 ENTRÉES PORTC

PORTD=0X00; // RESET PORTD
TRISD=0X00; // ACTIVE LES 8 ENTRÉES PORTD

actual_pulse_position = 0;
turncnt = 0;
ab = 0;
POSCNTH = 0;
POSCNTL = 0;
}
void LCDINIT(){
LCD_INIT(); // INITIALIZE LCD
LCD_CMD(_LCD_CLEAR); // CLEAR DISPLAY
LCD_CMD(_LCD_CURSOR_OFF); // CURSOR OFF
}
void PRINTCOUNT(){
actual_pulse_position =0; // CLEAR actual_pulse_position
actual_pulse_position = poscnth<<8 ; // LOAD CNT MSB THEN SHIFT LEFT 8BITS
actual_pulse_position+= poscntl; // ADD CNT LSB
WORDTOSTR(actual_pulse_position,txt_poss );
ab = (turncnt * encoderTurnPulseX4)+ actual_pulse_position; // CALCUL TOTAL PULSE
LONGTOSTR(ab, txt_cnt);
}
void PRINTTURN(){

}
void PRINTDIRECTION(){
if (direction == 1){
RA4_BIT = 1;
RA5_BIT = 0;
txt_dir[0] = '>';
txt_dir[1] = '>';
}
else{
RA4_BIT = 0;
RA5_BIT = 1;
txt_dir[0] = '<';
txt_dir[1] = '<';
}
}
void PRINTERR(){
if (QERR_BIT == 1){
err++;
}
}
void PRINTSPEED(){
}
void PRINTtoLCD(){
lcd_cmd(_LCD_CLEAR);
LCD_OUT(1,1,txt_poss);
LCD_OUT(2,1,txt_cnt);
LCD_OUT(2,13,txt_dir);
}
void MAIN(){
INIT();
LCDINIT();
do{
switch (scan){
case 0: PRINTCOUNT(); break;
case 5: PRINTERR(); break;
case 10: PRINTDIRECTION(); break;
case 15: PRINTTURN(); break;
case 20: PRINTSPEED(); break;
case 25: PRINTtoLCD();break;
}
scan++;
if (scan>25){scan=0;}

}while(1);
}
static void interrupt(){
if(IC2QEIF_BIT=1){
if (POSCNTH == 0){turncnt++;}
else {turncnt--;}
}
IC2QEIF_BIT = 0;
}

Post Reply

Return to “mikroC General”