- Joined
- Jul 4, 2009
- Messages
- 16,532
- Helped
- 5,157
- Reputation
- 10,347
- Reaction score
- 5,215
- Trophy points
- 1,393
- Location
- Aberdyfi, West Wales, UK
- Activity points
- 139,786
The 18F4520 is fine, no need to use a different PIC.
I did find a bug in Oshonsofts simulator through and it stops your program working properly. When Timer0 is configured in 16 bit mode it doesn't carry from bit 7 of TMR0L to bit 0 of TMR0H as it should. I have modified your program so it shows the state of the zero crossing inputs properly and also the phase. To get around the timer problem I just chaged to Timer 1 instead of Timer 0.
It seems to work in simulation but you will have to try it in real life for yourself. I suspect the LCD routines will make the reading inaccurate because of the delays they introduce but 'one step at a time'. If you get results, I will change it back to using interrupts so the signals get immediate attention.
Brian.
I did find a bug in Oshonsofts simulator through and it stops your program working properly. When Timer0 is configured in 16 bit mode it doesn't carry from bit 7 of TMR0L to bit 0 of TMR0H as it should. I have modified your program so it shows the state of the zero crossing inputs properly and also the phase. To get around the timer problem I just chaged to Timer 1 instead of Timer 0.
It seems to work in simulation but you will have to try it in real life for yourself. I suspect the LCD routines will make the reading inaccurate because of the delays they introduce but 'one step at a time'. If you get results, I will change it back to using interrupts so the signals get immediate attention.
Code:
Define LCD_LINES = 4
Define LCD_CHARS = 16
Define LCD_BITS = 8 'allowed values are 4 and 8 - the number of data interface lines
Define LCD_DREG = PORTC
Define LCD_DBIT = 0 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 0
Define LCD_EREG = PORTD
Define LCD_EBIT = 2
Define LCD_RWREG = PORTD 'set to 0 if not used, 0 is default
Define LCD_RWBIT = 1 'set to 0 if not used, 0 is default
Define LCD_COMMANDUS = 2000 'delay after LCDCMDOUT, default value is 5000
Define LCD_DATAUS = 100 'delay after LCDOUT, default value is 100
Define LCD_INITMS = 100 'delay used by LCDINIT, default value is 100
'the last three Define directives set the values suitable for simulation; they should be omitted for a real device
Dim timer As Single
Dim phase As Single
Dim scale_factor As Single
Lcdinit 0 'initialize LCD module; cursor is blinking
TRISB = 0x03 'TRISB has bits 0 And 1 As inputs
T1CON = 0x24 'T1CON enabled, 2 x 8 Bit, internal clock, prescale 1: 4
INTCON = 0x10
INTCON2 = 0xe0
INTCON3 = 0x08
ADCON1 = 0x0f
scale_factor = 277
main:
TMR1L = 0
TMR1H = 0
timer = 0
loop:
If PORTB.0 Then
T1CON.TMR1ON = 1
Lcdcmdout LcdLine1Home
Lcdout "VZCD ON "
Else
Lcdcmdout LcdLine1Home
Lcdout "VZCD OFF"
Endif
If PORTB.1 Then
T1CON.TMR1ON = 0
Lcdcmdout LcdLine2Home
Lcdout "IZCD ON "
timer = (TMR1H * 256) + TMR1L
phase = timer / scale_factor
Lcdcmdout LcdLine3Home
Lcdout "Phase ="
Lcdout #phase
Else
Lcdcmdout LcdLine2Home
Lcdout "IZCD OFF"
Endif
Goto loop
End