I have struggled for days to activate the internal pull-ups resistors of pic24jf64ga004 without success. I’m yet to know where I didn’t get right on the datasheets. I do simulation on Proteus 8. The pin I would like them pulled up are RC6, RC7, RC8, and RC9. I have connected button switches on these pins. They are gray during simulation. According to my knowledge with Proteus that mean they are neither connected to ground not to positive source. Kindly help me with my code bellow. There must be something I haven’t done right but cannot know yet. This is my first project with PIC24 families.
Also please check my interrupt coding. I’m not sure yet if I have done it right as without the pull-ups working I have no way to test it. I’m not yet clear with the way interrupts are handled in the PIC24 series. I fact the help files are really hard to me to understand that the 18F.
My VDDCORE/VICAP pin is connected to 10uF capacitor that goes to ground, and the DISVREG pin is connected to ground also. This is as per datasheet, to activate internal regulator, voltage of which also used by the pull-up resistors.
Hope someone will spot my mistake here and thank you all in advance.
Victor
Code: Select all
program Egg1
'Temp PV -------
dim dig1, dig2, dig3 as byte
'Temp SV -------
dim dig4, dig5, dig6 as byte
'%RH PV --------
dim dig7, dig8 as byte
'%RH SV --------
dim dig9, dig10 as byte
'Number of Days PV -----
dim dig11, dig12 as byte
'Number of Days SV ----
dim dig13, dig14 as byte
dim Dec_Val as byte 'Where to record decimal value after reading a sensor
dim BCD_Val as Word 'Where to record BCD Value after converting Decimal Value
' Registors for Humidity ++++++++++++++++++++++++++++++++
dim RH_Read0 as byte ' is this register really needed?
dim RH_Read1 as byte ' is this register really needed?
dim RH_Read2 as byte ' is this register really needed?
dim RH_Raw as longword ' place to save raw humidity readings (12bit default)
dim RH_Phy as byte ' place to save physical humidity readings
dim RH_BCD as byte ' register to save BCD value of humidity readings
' Registers for Hemperature ++++++++++++++++++++++++++++
dim Temp_Raw as word 'Place to save raw temperature data readings (14bit default)
dim Temp_Phy as byte ' Place to save temperature converted to physical temperature
dim Temp_BCD as byte ' Register to save BCD value of temperature
dim Temp_Lo_Byte as byte ' Low byte of the temperature
dim Temp_Hi_Byte as byte ' High byte of the temperature
' timer variables
dim counter as byte ' TMR1 Overflow counters
dim seconds as byte ' seconds counter
dim minutes as byte ' minutes counter
dim hours as byte ' hours count
dim days as byte ' Days counter
dim Days_BCD as byte ' Register for days BCD after conversion
' Timer Notes:
' TMR1 is prescaled to 1/8 hance at 8MHz system clock the timer frequence will
' be 2MHz/8, that is 250kHz.
' At 250kHz, as TMR1 register is 16bit, overflow will happen at 65536/250000,
' that is approximately 262ms. Thus, to get 1 second, TMR1 will overflow 3.816
' times, approximately 4 times.
' Hence, we'll increment our counter at every overflow four times (will create
' 1048ms) and declare that a second.
' external devices ++++++++++++++++++++++++++++++++++++++
'dim Turner as sbit at PORTE.0
' buttons operations ++++++++++++++++++++++++++++++++++++
' Common Registers:
dim SetSave as byte ' Setting status flug register
dim SelectReg as byte ' Register for sellection status
' %RH Registers:
dim RH_Reset_Val as byte ' Register holding %RH binary number
dim RH_SV_BCD as byte ' Register holding BCD value of SV %RH
' Days Registers:
dim DAYS_Reset_Val as byte ' Register holding Days binary number
dim DAYS_SV_BCD as byte ' Register holding BCD value of SV DAYS
' Temp Registers:
dim TEMP_Reset_Val as word ' Register holding Temp binary number
dim TEMP_SV_BCD as word ' Register holding BCD values of TEMP
' Flags +++++++++++++++++++++++++++++++++++++++++++++++++
dim UpdateFlag as byte ' If this register is FF, Update readings
'Sending commands to STH-21
sub procedure Temp_Read
I2C1_Start()
I2C1_Write(0x80) ' issue write command
I2C1_Write(0xf3) ' Command SHT21 to measure Temperature -no hold master mode
I2C1_Start()
I2C1_Write(0x81) ' Address SHT21 to read (address + W)
Temp_Hi_Byte = I2C1_Read(1) ' Read the first byte of temp measurement into Lo reg of temp
Temp_Lo_Byte = I2C1_Read(1) ' Read the second byte of temp measurement into Hi reg of tem
I2C1_Stop()
Temp_Phy = (-46.85 + 175.72 * (Temp_Raw/65536))
Temp_BCD = Dec2Bcd(Temp_Phy) 'Convert the decimal Temp to BCD
dig3 = Temp_BCD
dig2 =(Temp_BCD >> 4)
end sub
sub procedure RH_Read
I2C1_Start() ' start I2C communication
I2C1_Write(0x80) ' address SHT21 for write (address + W)
I2C1_Write(0xF5) ' Command SHT21 to measure Humidity -no hold master mode
I2C1_Start() ' Start new communication
I2C1_Write(0x81) ' address SHT21 to read (address + R)
RH_Raw =I2C1_Read(1) ' Test read Whole 24 bits from the sensor
'RH_Read2 = I2C1_Rd(1) ' read %RH
'RH_Read1 = I2C1_Rd(1)
'RH_Read0 = I2C1_Rd(0)
I2C1_Stop()
RH_Raw = (RH_Raw >> 10) 'Remove the checksum and status bits
RH_Phy = (-6 + 125 * (RH_Raw / 65536)) 'Convert the raw reading to decimal %RH
RH_BCD = Dec2Bcd(RH_Phy) 'Convert the decimal %RH to BCD
dig8 = RH_BCD
dig7 =(RH_BCD >> 4)
end sub
sub procedure TimeEventsUpdate
' Seconds
if counter = 4 then ' this is one second
counter = 0 ' clear counter
seconds = seconds + 1 ' increment seconds count
end if
' Minutes
if seconds = 60 then
seconds = 0 ' clear seconds count
minutes = minutes + 1 ' increment minutes count
' other actions:
' update system controls (heater, exhaust fan, humidity, display)
end if
' Hours
if minutes = 60 then
minutes = 0 ' clear minutes count
hours = hours + 1 ' increment hours count
' Other actions:
'Turner = not(Turner) ' reverse egg turner
end if
' Days
if hours = 24 then
hours = 0 ' clear hours count
days = days + 1 ' increment days count
'Other actions:
Days_BCD = Dec2Bcd(hours) 'Convert the decimal hours to BCD
dig12 = Days_BCD
dig11 =(Days_BCD >> 4)
end if
end sub
' PROCEDURES USED DURING RESETING OF THE INCUBATOR
'sellect what to set (Temp, %RH or Days)
sub procedure selector ' Number 1 to 3 indicate which desplay to set and
If SelectReg = 3 then ' animate during resetting.
SelectReg = 1
else
SelectReg = SelectReg + 1
end if
end sub
'Convert the decimal Reset Value of humidity to BCD
sub procedure RH_SV_Digits
RH_SV_BCD = Dec2Bcd(RH_Reset_Val)
dig10 = RH_SV_BCD
dig9 =(RH_SV_BCD >> 4)
UpdateFlag = $00
end sub
'Convert the decimal Reset value of Days to BCD
sub procedure DAYS_SV_Digits
DAYS_SV_BCD = Dec2Bcd(DAYS_Reset_Val)
dig14 = DAYS_SV_BCD
dig13 =(DAYS_SV_BCD >> 4)
UpdateFlag = $00
end sub
sub procedure TEMP_SV_Digits
TEMP_SV_BCD = Dec2Bcd16(TEMP_Reset_Val)
dig6 = TEMP_SV_BCD
dig5 = (TEMP_SV_BCD >> 4)
dig4 = (TEMP_SV_BCD >> 8)
UpdateFlag = $00
end sub
sub procedure KeyPress() iv IVT_ADDR_INTERRUPT78 ics ICS_AUTO
IFS1.CNIF = 0 'Clear Input Change Notification Interrupt flag
if TestBit(IFS1,CNIF) = 1 then ' if the interrupt comes form RB port change
if TestBit(PORTC, RB6) = 0 then ' and the cange comes from RB4 then,
Selector ' Sellect display digits to update
Sound_Play(2500, 20) ' beep when button BTN1 is pressed
else
if (TestBit(PORTC, RB7) = 0) and (SelectReg = 2) then '%RH is being reset
RH_Reset_Val = RH_Reset_Val + 1
Sound_play(2500, 20) ' beep when button BTN2 is pressed
UpdateFlag = $FF ' go to ISR part of the project.
else
if (TestBit(PORTC, RB7) = 0) and (SelectReg = 3) then ' DAYS is being reset
DAYS_Reset_Val = DAYS_Reset_Val +1 ' increment DAY reset value
Sound_play (2500, 20) ' beep when button BTN2 is pressed
UpdateFlag = $FF
else
if (TestBit(PORTC, RB7) = 0) and (SelectReg = 1) then ' TEMP is being reset
TEMP_Reset_Val = TEMP_Reset_Val + 1 ' increment TEMP reset value
Sound_play (2500, 20) ' beep when BTN2 is pressed
UpdateFlag = $FF ' Command the program to go to ISR
else
if (TestBit(PORTC, RB8) = 0) and (SelectReg = 2) then '%RH is being reset
RH_Reset_Val = RH_Reset_Val - 1
Sound_play(2500, 20) ' beep when button BTN2 is pressed
UpdateFlag = $FF ' command the program to go to ISR part of the project.
else
if (TestBit(PORTC, RB8) = 0) and (SelectReg = 1) then 'TEMP is being reset
TEMP_Reset_Val = TEMP_Reset_Val - 1
Sound_play(2500, 20) ' beep when button BTN2 is pressed
UpdateFlag = $FF ' command the program to go to ISR part of the project.
else
if (TestBit(PORTC, RB8) = 0) and (SelectReg = 3) then 'DAYS is being reset
DAYS_Reset_Val = DAYS_Reset_Val - 1
Sound_play(2500, 20) ' beep when button BTN2 is pressed
UpdateFlag = $FF ' command the program to go to ISR part of the project.
end if
end if
end if
end if
end if
end if
end if
end if
end sub
main:
'Main program
'Oscillator sellection and settings ------------
'Internal First Oscillator FRC is used
OSCCON = $00c0 ' Settings for the above hints
CLKDIV = $0000 ' Oscillator operate at 8MHz
' done with OSCILLATOR settings
'Initializing ports ----------------------------
TRISA = $0 ' PortA is OUTPUT
TRISB = $0 ' Pin RB3 of PortB is output. All other are inputs
TRISC = %0000001111000000 ' Seting I/O pins for portc
'ADC Confituration;
PORTA = $0 ' Clear PORTA
PORTB = $0 ' Clear PORTB
PORTC = $0 ' Clear PORTC
' Initialising port done
'Interrupt settings ---------------------------
'INTCON1: Interrupt nesting disable, control and status flags
'INTCON2: External inturrupt request control
'IFSx registers: All interrupt request flags
'IECx registers: All interrupt enable bits
'IPCx registers: Interrupt priority levels
'I'll check SR and CORCON CPU control register later if I will need
' need to change something there.
'Setting First Interrupt Control Registor (INTCON1) follows:
INTCON1.NSTDIS = 0 'Interrupt nessting is enabled
'Setting Second Interrupt Control register (INTCON2) follows:
INTCON2 = 0 'External interrupt trigger on positive egde
'Standard vectors are used
'Interrupt enabling follows:
'NOTE: All interrupt enabling bits are in registers IEC0 - IECO4
IEC3.RTCIE = 1 'Real-Time Clock/Calender Interrupt is enabled
IEC1.CNIE = 1 'Input Change Notification Interrupt is enabled
'Clearing Interrupt flags
IFS0 = 0 'Clear all interruptS in IFS0
IFS1 = 0 'Clear all interrupts in IFS1
IFS2 = 0 'Clear all interrupts in IFS2
'Enabling Input Change Notification and Pull up resistor for switch buttons
'Associated registors are:
' (1) CNEN1 and CNEN2 -for interrupt enabling of sellected pins
' (2) CNPU1 and CNEN2 -for pull up resistors
CNEN2 = %0000000000011110 'eneble input change notificatin for switch buttons
CNPU2 = %0000000000011110 'Enabling pull-up resistors for above CN pins
'Initializing sounds ----------------------------
'Sound_Init(PORTE, 2) ' Pin 7 of port D is used for sound
' Initilising sound done
'Initializing IC2 -------------------------------
I2C1_Init(100000) ' Initialize IC2 at 100kHz clock speed
'*******************************************************************************
'************************** START THE MUSIC ************************************
'*******************************************************************************
ISR: 'If TMR1 Interrupt occur, come here
RH_SV_Digits 'Converts %RH setting to BCD
DAYS_SV_Digits 'Converts DAYS setting to BCD
TEMP_SV_Digits 'Converts TEMP settings to BCD
music:
' Uncomment the following
'RH_Read
'Temp_Read
TimeEventsUpdate
'sound_play(250, 20)
'***************Digit Multiplexer*****************
'Temp PV ----------------------------------------
PORTB = dig1
LATC.0 = 1
delay_ms(3)
LATC.0 = 0
'----------
PORTB = dig2
LATC.1 = 1
delay_ms(3)
LATC.1 = 0
'----------
PORTB = dig3
LATC.2 = 1
delay_ms(3)
LATC.2 = 0
'Temp SV -----------------------------------------
PORTB = dig4
LATC.3 = 1
delay_ms(3)
LATC.3 = 0
'----------
PORTB = dig5
'RA4 is open drain. hance I connect a pull-up resestor on it that it can work
' like other pins of the port.
LATC.4 = 1
delay_ms(3)
LATC.4 = 0
'----------
PORTB = dig6
LATC.5 = 1
delay_ms(3)
LATC.5 = 0
'RH PV -------------------------------------------
PORTB = dig7
LATB.10 = 1
delay_ms(3)
LATB.10 = 0
'----------
PORTB = dig8
LATB.11 = 1
delay_ms(3)
LATB.11 = 0
'RH SV --------------------------------------------
PORTB = dig9
LATB.12 = 1
delay_ms(3)
LATB.12 = 0
'----------
PORTB = dig10
LATB.13 = 1
delay_ms(3)
LATB.13 = 0
'Days PV -----------------------------------------
PORTB = dig11
LATB.14 = 1
delay_ms(3)
LATB.14 = 0
'----------
PORTB = dig12
LATB.15 = 1
delay_ms(3)
LATB.15 = 0
'----------
'Days SV -----------------------------------------
PORTB = dig13
LATA.7 = 1
delay_ms(3)
LATA.7 = 0
'----------
PORTB = dig14
LATA.10 = 1
delay_ms(3)
LATA.10 = 0
'----------
' Now check if there are update
if UpdateFlag = $FF then ' if there is flag for development
goto ISR
else
goto music
end if
end.