PDA

View Full Version : changing code from 16F to 18F microprocessor



SCC_699
- 12th May 2008, 18:33
I am relatively new to the chip programming game and I have some of my first code below for your review. I am trying to time some liquid flow between sensors. I found large discrepancies from count to count (value variable). Basically, I see a time decay. I tried resetting the flags for the TMRO counter, to no avail.

I thought I would try a chip with more memory (current chip is PIC16f84a, new chip is PIC18f2620), but I get a compiler error. I thought the pin assignments were off. I'm sure there are quite a few things I'm missing any help would be appreciated.


dummy var word ' div 32 variable
newdum var word
newdum3 var word

value var word ' raw counts
num var word
den var word
clear

input portb.5 'start signal first sensor
input portb.6 'stop signal second sensor
input portb.7 'resume
output portb.0 'solenoid

high 0

Pause 500 ' Wait 0.5s for LCD to startup
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Chromatography Inc." 'display message first line
Lcdout $fe, $c0, "Flow Meter2" ' Display meaasege second line
pause 4000
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "By Helcio" 'display message first line
Lcdout $fe, $c0, "Ver 1.0" ' Display meaasege second line
pause 4000

loop:

Lcdout $fe, 1 ' Clear LCD screen
value = 0
num = 0
den = 0
low 0
Lcdout "Waiting for" 'display message first line
Lcdout $fe, $c0, "Pump" ' Display meaasege second line

loop2:
' checks for first sensor
if portb.5 = 1 then loop2 'check start input
lcdout $fe, 1 ' Clear LCD screen
Lcdout "Measuring" ' Display message

loop3:
'counts while liquid is between sensors
Pause 1 ' Wait 0.001 second
value = value + 1 'count time delay
if value > 60000 then loop5

if portb.6 = 1 then loop3 'check stop input

high 0

if value < 2 then loop6

'num = value / 1000
'den = value // 1000

Lcdout $fe, 1 ' Clear LCD screen
'Lcdout "t = ", dec num, ".", dec den, "s" ' display time

Lcdout "t = ", dec value ' display time


dummy = value * 369 'm value of equation from slope equation y=mx+b


newdum = div32 1000

newdum3 = 5441 - newdum 'b value of equation

num = newdum3 / 1000
den = newdum3 // 1000
Lcdout $fe,$c0, "r = ", dec num, ".", dec den, "mL/min" ' ratio time


loop4:

if portb.7 = 0 then loop4 'check again input
goto loop
loop5:
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Pump to slow"
Lcdout $fe,$c0, "Increase speed"
loop7:
if portb.7 = 0 then loop7 'check again input
goto loop

loop6:
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Pump to fast"
Lcdout $fe,$c0, "Decrease speed"
loop8:
if portb.7 = 0 then loop8 'check again input
goto loop

Thanks for your help

skimask
- 12th May 2008, 19:35
I thought I would try a chip with more memory (current chip is PIC16f84a, new chip is PIC18f2620), but I get a compiler error. I thought the pin assignments were off. I'm sure there are quite a few things I'm missing any help would be appreciated.
Define compiler error better and you might get a bit more help...
And which version of PBP?

SCC_699
- 12th May 2008, 19:49
I am using PBP ver 2.47
the compiler error is "Configuration programming error at 0000"

Originally compiled with chip PIC16f84a tried to recompile by selecting PIC 18F2620 get the above
thanks

Acetronics2
- 12th May 2008, 19:50
Hi, The following is ...

just a simple 18F2520 header !!!




'************************************************* ****************************
'************************************************* ****************************
'
'Defines
'************************************************* ****************************

DEFINE OSC 4
DEFINE BUTTON_PAUSE 250
DEFINE ADC_BITS 10
'DEFINE LCD_COMMANDUS 2000
'DEFINE LCD_DATAUS 50


' Define LCD connections

DEFINE LCD_DREG PORTC ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 5 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 6 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)

'
'
'************************************************* ****************************
' Constantes
'************************************************* ****************************

ExtOSC con 32768 'Quartz Horloge "temps" en Hz

Tension_ con 0 'Correspondance pour Canaux ADC
TempA_ con 1
TempH_ con 2
Dureetonte con 1 ' de -1h à +59 mn ... ( 1/2 durée !!! )
Kelvin con 273

'*************************** seuils Alarmes ********************************

AlmVolt con 15 * 100 '>= Tension trop élevée 1/100 V
AlmRpm con 3600 '>= Régime trop élevé T/Mn
AlmAir con 90 + 273 '>= Temp Air Trop élevée °K ^ 82
AlmHuileL con 0 + 273 '<= Temp Huile trop basse °K
AlmHuileH con 100 + 273 '>= Temp Huile trop élevée °K

'************** Periodicités d'Entretien ( ordre croissant ! ) *************

Service1 con 10 ' Heures
ServiceV con 20
Service2 con 25
Service3 con 50
Service4 con 100
Service5 con 300

DATA (0),0 (29) 'Raz des Mémoires si reprogrammation

'************************************************* ****************************
' 1er préchargement 22H31M29S
'************************************************* ****************************

DATA @0, $1
DATA @4, $1
DATA @6, $16 ' 22 Heures
DATA @10,$1F ' 31 Minutes
DATA @12,$1D ' 29 Secondes
DATA @14,$FF
DATA @16,$0D ' 3583 Tr/mn
DATA @22,$53 ' 83°C Air
DATA @26,$59 ' 89°C Huile

VERSION con 1 'N° de la révision

'************************************************* ****************************
'************************************************* ****************************
'Config processeur
'************************************************* ****************************
'

'@ __CONFIG _CONFIG1L, _WDT_OFF_1L & _WDTPS_128_1L
@ __CONFIG _CONFIG1H, _IESO_OFF_1H & _OSC_XT_1H & _FCMEN_OFF_1H
@ __CONFIG _CONFIG2L, _BOREN_OFF_2L & _PWRT_ON_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_128_2H
'@ __CONFIG _CONFIG3L, _WDT_OFF_3L & _WDTPS_128_3L
@ __CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTBE_3H
@ __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L & _XINST_OFF_4L
'@ __CONFIG _CONFIG4H, _WDT_ON_4H & _WDTPS_128_4H
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
@ __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H

'************************************************* ****************************
'Config TIMERs

T0CON = %00000010 ' 16Bits 1/8 prescaler, Horloge Système, OFF
' Utilisé pour détection sous regime ou arret

T1CON = %00000000 '1/1 prescaler, Quartz 32,768
' *** Géré par Elapsed Timer ***

T3CON = %01110001 ' CCP SUR TMR3,1/8 prescaler,Horloge système,ON
' Mesure de la vitesse
'************************************************* ****************************
'Config CCP1 arret CCP2

CCP1CON = %00000101 ' Capture sur front montant

CCP2CON = %00000000 'inhibition module CCP2

'************************************************* ****************************
'Inhibition SSP

SSPCON1.5 = 0

'************************************************* ****************************
'Inhibition Comparateurs

CMCON = 7

'************************************************* ****************************
'Inhibition Reference interne

CVRCON = 0

'************************************************* ****************************
'Config Interruptions

INTCON = %10000000
INTCON2 = %11110000
INTCON3 = %00000000

PIE1 = %00000100
PIE2 = %00000000

IPR1 = %00000001
IPR2 = %00000000

RCON = %10000000

'************************************************* ****************************
'Config ADC
'************************************************* ****************************

ADCON0 = %11000000 '
ADCON1 = %00011011 ' 3/1
ADCON2 = %10100110 ' Right Just., 8Tad, clock /64

'************************************************* ****************************
'************************************************* ****************************
'Config et Prepositionnement E/S
'************************************************* ****************************

PORTA = %00110000
PORTB = %00010000
PORTC = %00001000
'PORTD = 0
'PORTE = 0

TRISA = %00011111
TRISB = %00000110
TRISC = %00000111
'TRISD = %00000000
'TRISE = %00000000
'
'************************************************* ****************************
'Alias E/S
'************************************************* ****************************

'MCLR/ sur Broche 1 par R/C/D

Voltage var PORTA.0 'TENSION Batterie / 4
TempA var PORTA.1 'LM335
TempH var PORTA.2 'LM335
Vref var PORTA.3 'TL431 = 4.096v ...
Raz var PORTA.4 'JUMPER Raz/Programmation
AlRef var PORTA.5 'Alimentation référence TL431

Buzzer var PORTB.0
Contact var PORTB.1 ' + 12 v sous clé ( / 4 = 2 - 4 v !!! )
Selection var PORTB.2 'Poussoir sélection Affichage
Signal var PORTB.3 'Sortie Survitesse
AlLCD var PORTB.4 'Alimentation LCD BS 250 !!! 0 = ON : 1 = OFF
RS var PORTB.5
E var PORTB.6
Alm var PORTB.7 'Sortie Alarme

' var PORTC.0 'Qz 32768
' var PORTC.1 ' "
Speed var PORTC.2 'Entrée compte-tours 1C/4T
Alim var PORTC.3 'Autoalimentation
D4 var PORTC.4
D5 var PORTC.5
D6 var PORTC.6
D7 var PORTC.7

'Buzzer var PORTD.0 'Sortie commande Buzzer
'Signal var PORTD.1 'Voyant Survitesse
'RS var PORTD.2
'E var PORTD.3
'D4 var PORTD.4
'D5 var PORTD.5
'D6 var PORTD.6
'D7 var PORTD.7

' var PORTE.0
' var PORTE.1
'AlRef var PORTE.2

'************************************************* ****************************
'Types Variables
'************************************************* ****************************
'
Result var WORD

Volt var WORD
Tension var WORD
Tensionmoy var WORD

Tair var WORD
TAMax var WORD ' Valeur Maxi Split
TairMax var WORD ' Valeur Maxi mémorisée

Thuile var WORD
THMax var WORD ' Valeur Maxi Split
THuileMax var WORD ' Valeur Maxi mémorisée

.
.
.



Now, you know what you have to do ...

hé,hé

Alain

SCC_699
- 12th May 2008, 20:53
Do I need to define / configure all of the ports on the chip or just the ports used in the program? I don't recall defining the ports that way for the 16f and it seemed to compile fine and run well except for the wide range in the counts from the timer loop.

sougata
- 13th May 2008, 06:48
Hi,

Have you tried with MPASM ? Could you actually locate the "18F2620.INC" . Default configurations are always there. So I seriously doubt why the program would not compile and issue configuration error. Here it does compile okay.

Acetronics2
- 13th May 2008, 08:15
Do I need to define / configure all of the ports on the chip or just the ports used in the program? I don't recall defining the ports that way for the 16f and it seemed to compile fine and run well except for the wide range in the counts from the timer loop.

Hi,

The compiler did it for you ... as your first projects were using the DEFAULT config provided !!!
For "bigger" chips the compiler DEFAULT config lines are not so numerous as the offered features ... it's you to define what you want !!!

You, of course, have to define all ports as I or Os , and initialize them ... if you do not want to get unexpected behaviour.

May be a "midrange" series chip as a second one ( if i understood your post ...) as a 16F628, 16F648 ,16F876a or 16F877a could help for the transition ... with their "limited bunch" ( lol ! ) of peripherals ...

Alain

SCC_699
- 14th May 2008, 04:06
Alain Thanks for the reply. I would like to use the pic16f877 (since I have a few onhand) but I was concerned it would behave similar to the 16f84a that I am currently using.

I was experiencing a large variation in the incremented variable despite the fact that the time between the sensors was very precise. (I use the raw counts in a flow calculation.) The actual time between the sensors varies from 2 - 5 seconds depending on the flow. The raw counts for the variable (value) are approximately 33,000 for 1 ml/min flow. The variablity was more than 25% between successive measurements and I also observe a decay in the counts.

I thought a chip with more capacity would help. I realize now that it is a large leap from one to the other since I will have to rewire the processor board to the 18f pinout.

If there is a chip you could recommend that would meet the data and calculation needs while trimming down the unused peripherals that would be appreciated as well.

Thanks

Ioannis
- 14th May 2008, 06:50
I think the closest to F877 is the 18F452. It is pin to pin compatible and has twice the memory. Once I remember porting the almost 7,5K of program directly to the F452 with a couple of DEFINEs on top. Piece of cake.

Ioannis

Acetronics2
- 14th May 2008, 09:13
I thought a chip with more capacity would help. I realize now that it is a large leap from one to the other since I will have to rewire the processor board to the 18f pinout.

If there is a chip you could recommend that would meet the data and calculation needs while trimming down the unused peripherals that would be appreciated as well.

Thanks

Hi, You can try the 18F1320 instead of a 16F84 ... pinout is COMPATIBLE ( care ... pins names are not the same !!! ... so you only have to rename them . )

Alain

skimask
- 14th May 2008, 13:03
I think the closest to F877 is the 18F452. It is pin to pin compatible and has twice the memory. Once I remember porting the almost 7,5K of program directly to the F452 with a couple of DEFINEs on top. Piece of cake.
Ioannis
Agreed 100%.
Started out a project a long time ago on a 16F877A. Ran out of memory, plugged in an 18F452, changed a couple of define's, a few register settings, ran great. Ran out of memory again, plugged in an 18F4620, changed a few more register settings...Same program, ran like a champ (then ran out of code space again, built up an 80pin TQFP-> 40 pin DIP adapter for an 18F8722, plugged it in, changed 1 or 2 register settings, doubled the code space, ran great again).

Ioannis
- 14th May 2008, 13:21
Agreed 100%.
Started out a project a long time ago on a 16F877A. Ran out of memory, plugged in an 18F452, changed a couple of define's, a few register settings, ran great. Ran out of memory again, plugged in an 18F4620, changed a few more register settings...Same program, ran like a champ (then ran out of code space again, built up an 80pin TQFP-> 40 pin DIP adapter for an 18F8722, plugged it in, changed 1 or 2 register settings, doubled the code space, ran great again).

... then run out of memory again and plugged a ... x86 core 2 with 2gb of memory!

Ioannis

skimask
- 14th May 2008, 13:25
... then run out of memory again and plugged a ... x86 core 2 with 2gb of memory!
Ioannis
I could run the 18F8722 in external mode and 2GB of flash....and skip the Core2 altogether :D

But that's what I get when I kept adding crap on top of crap on top of crap :D
I went thru the code awhile back and tried a fair amount of optimizing. Turns out I could've stuck with the '452 if I'd have just been a bit more diligent in the code writing in the first place.

Ioannis
- 14th May 2008, 13:27
But that's what I get when I kept adding crap on top of crap on top of crap :D

I think thats called spaggeti programming! You are lazy to optimize it once then it grows linearly, or better exponentially, full of crap! I 've been there too!

Ioannis