Dear / Les / PIC-IGNORANT
I have spent the day trying to learn the A/D convertor in 12 bit mode
I studied for hours the 33E A/D Converter manual
I could not get the proton 24 command Myresult = ADIN 0 command to work even when inculde "adin.inc" it just hung the MCU at that command
So i decided to study the 33E A/D ref manual I got it working and I have just started off reading one channel at 12 bits.
I have wired a 5 k pot to the 3.3v supply and the wiper arm of the pot goes to up to AN5 rb.5 pin pin 20 of the 100pin tqfp
and it works 0 - 3.3v = 0-4095
Code shown Below
Also I have got the hardware PWM working on PWM1H / porte.1 / pin 94 0f the 11 pin tqfp
the pwm just sits at a fixed duty cycle for now just to test the PWM works nice at 5 Khz
PDC1 = 2000 sets the duty cycle to 60% just to test the pwm module
Code:
'TMR Tripple Modular Redundancy
'Tripple MCU Voting system 3 MCU's fast communication
Device = 33EP512MU810 '100 pin tqfp
Declare Xtal = 140 'was 140
'PLL_Setup(76, 2, 2, $0300) 'only examples can find are for internal oscillator
'the above macro locks PLL but can nott find an example for ext 8MHz Xtal
CLKDIV.7 = 0 'PLL POST (set to divide by 2)
CLKDIV.6 = 0 'PLL POST (set to divide by 2)
CLKDIV.4=0 ' PLL POST (set to divide by 2)
CLKDIV.3=0 ' PLL POST (set to divide by 2)
CLKDIV.2=0 ' PLL POST (set to divide by 2)
CLKDIV.1=0 ' PLL POST (set to divide by 2)
CLKDIV.0=0 ' PLL POST (set to divide by 2)
PLLFBD.8=0'70MIPS
PLLFBD.7=0
PLLFBD.6=1
PLLFBD.5=0
PLLFBD.4=0
PLLFBD.3=0
PLLFBD.2=1
PLLFBD.1=0
PLLFBD.0=0
Config FICD = ICS_PGD1,JTAGEN_OFF
Config FPOR = ALTI2C1_ON,BOREN_OFF,FPWRT_PWR32
Config FWDT = WDTPOST_PS256,PLLKEN_OFF,WINDIS_OFF,FWDTEN_OFF
Config FOSC = POSCMD_HS,OSCIOFNC_OFF,FCKSM_CSECME
Config FOSCSEL = FNOSC_PRIPLL,IESO_OFF
Config FGS = GWRP_OFF
Config FAS = AWRP_OFF,APLK_OFF
'-------------------------------------------------------------------------
'Output PORTG.15 cant use that its for the bootloader mode !!
Input PORTF.12 'pin 40 RP108 U1RX
Output PORTF.13 'pin 39 RP109 U1TX
'----------U1 TX RX Setup --------------------------------------------------
U1MODEbits_STSEL = 0 '1 STOP BIT
'U1MODEbits_PDSEL = 0 'NO Parity, 8 data COMPILER WONT RECOGNISE COMMAND
U1MODEbits_ABAUD = 0
U1MODEbits_brgh = 0
U1BRG = 4 '37=115200'75=56700bps '(140mhZ / 115,200)/16 - 1 = 74.95 ~ 75 INTEGER
'DECIDED NOT TO HAVE INTERRUPT DRIVEN TX FOR NOW MIGHT USE LATER TO OPTIMISE
'u1sta.15 = 0 'interrupt after one tx character
'u1sta.13 = 0 'is transmitted
'IEC0bits_U1TXIE = 1 'enable tx interrupt
u1modebits_uarten = 1 'enable uart
U1STA.10 = 1 'Enable uart TX Bit 10
RPOR12 = 0 'clears
RPOR12 = %0000000000000001 ' pps output reg mapped u1tx pin 39
RPINR18 = 0 'CLEARS
RPINR18 = %0000000001101100 'pps output reg mapped U1RXPIN 40
Clear IEC0bits_U1RXIE 'clear U1 RX Interrupt
Clear IFS0bits_U1RXIF 'clear U1 RX Interrupt flag
Set IEC0bits_U1RXIE 'Set U1 RX Interrupt
U1STA.7=0'0 interrupt is set when any character is recieved
U1STA.6=0'x interrupt enabled when any character is recieved
'-------- end of u1 tx and rx setup ----------------------------------
'--------- u2 TX and RX setup ----------------------------------------
Input PORTD.14 'pin 47 RPI78 U2 RX 'RPI78 not RP178!!!!!
Output PORTD.15 'pin 48 RP79 U2 TX
U2MODEbits_STSEL = 0 '1 STOP BIT
'U2MODEbits_PDSEL = 0 'NO Parity, 8 data COMPILER WONT RECOGNISE COMMAND
U2MODEbits_ABAUD = 0
U2MODEbits_brgh = 0
U2BRG = 4'37 '37=115200'75=56700bps '(140mhZ / 115,200)/16 - 1 = 74.95 ~ 75 INTEGER
'DECIDED NOT TO HAVE INTERRUPT DRIVEN TX FOR NOW MIGHT USE LATER TO OPTIMISE
'u2sta.15 = 0 'interrupt after one tx character
'u2sta.13 = 0 'is transmitted
'IEC0bits_U2TXIE = 1 'enable tx interrupt
u2modebits_uarten = 1 'enable uart
U2STA.10 = 1 'Enable uart TX Bit 10
RPOR4 = 0 'clears
RPOR4 = %0000000000000011 ' pps output reg mapped u2tx pin 48
RPINR19 = 0 'CLEARS
RPINR19 = %0000000001001110'pps input reg mapped U1RXPIN 40
Clear IEC1bits_U2RXIE 'clear U1 RX Interrupt
Clear IFS1bits_U2RXIF 'clear U1 RX Interrupt flag
Set IEC1bits_U2RXIE 'Set U1 RX Interrupt
U2STA.7=0'0 interrupt is set when any character is recieved
U2STA.6=0'x interrupt enabled when any character is recieved
'-------- end of U2 Tx and Rx setup ----------------------------------
'''''''''''' U3 Tx and Rx Setup --------------------------------------
Input PORTG.0 'pin 90 RP112 U3 RX '
Output PORTG.1 'pin 89 RP113 U2 TX
'U3MODEbits_STSEL = 0 '1 STOP BIT
''U2MODEbits_PDSEL = 0 'NO Parity, 8 data COMPILER WONT RECOGNISE COMMAND
'U3MODEbits_ABAUD = 0
'U3MODEbits_brgh = 0
'U3BRG = 37'37 '37=115200'75=56700bps '(140mhZ / 115,200)/16 - 1 = 74.95 ~ 75 INTEGER
'DECIDED NOT TO HAVE INTERRUPT DRIVEN TX FOR NOW MIGHT USE LATER TO OPTIMISE
'u2sta.15 = 0 'interrupt after one tx character
'u2sta.13 = 0 'is transmitted
'IEC0bits_U2TXIE = 1 'enable tx interrupt
'u3modebits_uarten = 1 'enable uart
'U3STA.10 = 1 'Enable uart TX Bit 10
Declare Hserial3_Baud = 115200'9600
Declare Hrsout3_Pin = PORTG.1
Declare HRSin3_pin = PORTG.0
RPOR13 = 0 'clears
RPOR13 = %0000000000011011 ' pps output reg mapped u3tx pin 89
RPINR27 = 0 'CLEARS
RPINR27 = %0000000001110000'pps input reg mapped U3RXPIN 90
'Clear IEC5bits_U3RXIE 'clear U1 RX Interrupt
'Clear IFS5bits_U3RXIF 'clear U1 RX Interrupt flag
'Set IEC5bits_U3RXIE 'Set U1 RX Interrupt
'U3STA.7=0'0 interrupt is set when any character is recieved
'U3STA.6=0'x interrupt enabled when any character is recieved
'-------- end of U3 Tx and Rx setup ----------------------------------
Output PORTG.13 'pin 97 Toggle pin see how fast MCU executes loop on scope
'''''''''''' synchronise the 3 mcus with hardwired logic I/O Pins logical
Input PORTD.13 'checks if other MCU has recieved ful packet
Input PORTD.12 'checks if other MCU has recieved full packet
Output PORTG.14 'tells other 2 mcus full valid packet recieved
PORTG.14 = 0 'initially clear sync control
'''''''''''' PWM SETUP ''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' PTCON: PWM Time Base Control Register ''''''''''''''''''''''''''''''''''''
PTCONbits_PTEN = 0 ' timer enable bit disable initially
'Prescalar 1 : 8
PTCON2 = %011 '011 = divide by 8
'mode selection
PWMCON1 = $0000 'PTPER holds period count of pwm
'independent mode enable
IOCON1 = $CC00 'fig 14-35 of pwm manual
'$CC00 = PENH high, PENL high, PMOD: = 11 true independednt pwm o/p
''''' PTPER: pwm timebase period register
PTPER = 3500 'period value bits 3500 = 5.00 Khz frequency
PTCONbits_PTEN = 1 'enable pwm
PDC1 = 2000 ' 60 % duty cycle
' works perfectly get nice solid square wave
'duty is set by PDC1 0-3000 =0-100% Duty Cycle
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''' A/D convertor setup '''''''''''''''''''''''''''''''''''''''''''
'trisb.0 = 1 'input an0
'Declare Adin_Tad = c128_FOSC 'FOSC / 128
'Declare Adin_Stime = 100 '10 us
'Include "ADin.inc" 'load the ADin replacement routines into the program
'Dim MyResult As Word
'AD1CON2 = 0 ' Avdd, Avss, MUX A Only
'ANSELB = %0000000000000001 'ANO on port b
'
' from microchip 33E A/D Reference Manual
' Manual conversion and manual sample page DS70621c-page 16-20
'set port configuration
ANSELA = $0000
ANSELB = $0000
ANSELC = $0000
ANSELD = $0000
ANSELE = $0000
ANSELG = $0000
ANSELBbits_ANSB5 = 1'AN5 on portb.5 pin 20
'
'initialise and enable adc module
AD1CON1 = $0000
AD1CON2 = $0000
AD1CON3 = $000F
AD1CON4 = $0000
AD1CHS0 = $0005
AD1CHS123 = $0000
AD1CSSH = $0000
AD1CSSL = $0000
AD1con1bits_ADON = 1
ad1con1Bits_ad12b = 1 'set for 12 bit mode
DelayUS 20
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim a As Float 'float var1
a = 1000
Dim b As Float 'float var2
b = 10000
Dim c As Float 'float var3
c = 100000
Dim loop As Byte 'floating point repeat loop pointer
loop = 0
Dim d As Word 'test variable
d = 1000
Dim REC As Byte 'char recieved U1 Rx Interrupt
REC = 0
Dim REC1 As Byte 'char recieved U2 Rx Interrupt
REC1 = 0
Dim my_demand As Word 'word1
my_demand = 1000
Dim my_demandA As Word 'word1
my_demandA = 0
Dim my_demandB As Word 'word1
my_demandB = 0
Dim my_feedback As Word 'word2
my_feedback = 2000
Dim my_feedbackA As Word 'word2
my_feedbackA = 0
Dim my_feedbackB As Word 'word2
my_feedbackB = 0
Dim my_error As Word 'word3
my_error = 3000
Dim my_errorA As Word 'word3
my_errorA = 0
Dim my_errorB As Word 'word3
my_errorB = 0
Dim my_current1 As Word 'word4
my_current1 = 4000
Dim my_current1A As Word 'word4
my_current1A = 0
Dim my_current1B As Word 'word4
my_current1B = 0
Dim my_current2 As Word 'word5
my_current2 = 5000
Dim my_current2A As Word 'word5
my_current2A = 0
Dim my_current2B As Word 'word5
my_current2B = 0
Dim loop1 As Byte
loop1 = 0
Dim intout [13] As Byte
Dim bufcount As Byte
bufcount = 0
Dim bufcount2 As Byte
bufcount2 = 0
Dim tx1 [13] As Byte
Dim tx2 [13] As Byte
Dim sendrecieved As Dword
sendrecieved = 0
'timer1 initialise stolen from a c program I wrote for 33fj32gp202
T1CONbits_TON = 0';
T1CONbits_TCS=0';
T1CONbits_TGATE =0';
'T1CONbits_TCKPS = 0b00';
TMR1=0x00';
PR1 = 297'250'1000'; '// PIR settings PIR = 500 was 8uS /PIR = 1600 was 25uS /PIR = 65000 was 1ms
'//IPC1bits.T1IP = 0x01;
'//IFS0bits.T1IF = 0;
IEC0bits_T1IE = 1';
'T1CONbits_TON = 1';
'timer0 initialise stolen from a c program I wrote for 33fj32gp202
T2CONbits_TON = 0';
T2CONbits_TCS=0';
T2CONbits_TGATE =0';
'T1CONbits_TCKPS = 0b00';
TMR2=0x00';
PR2 = 27000'18000'300'1000'; '// PIR settings PIR = 500 was 8uS /PIR = 1600 was 25uS /PIR = 65000 was 1ms
'//IPC1bits.T1IP = 0x01;
'//IFS0bits.T1IF = 0;
IEC0bits_T2IE = 1';
'T2CONbits_TON = 1';
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim flag As Byte
flag = 0
Dim gotdata As Byte
gotdata = 0
Dim flag2 As Byte
flag2 = 0
Dim reading As Word
reading = 0
Main:
''''''''''''''''''''''coms to hyper terminal to show characters recieved
Inc sendrecieved
Select sendrecieved
Case 100
my_demandA.LowByte = tx1[1]
my_demandA.HighByte = tx1[2]
my_demandB.LowByte = tx2[1]
my_demandB.HighByte = tx2[2]
'HRSOut3 Dec my_demandA,13
'HRSOut3 Dec my_demandB,13
'***** A/D Conversio 12 bits channle AN5 portb.5 pin 20 of 100 pins
AD1CON1bits_SAMP = 1 'start sampling
DelayUS 10 'allows a 10uS Sampling time
AD1con1bits_samp = 0 'start the conversion
holdAN0:
If AD1CON1bits_DONE = 1 Then ' 1 if adc not busy
reading = ADC1BUF0 'grabs a/d converted value
Else 'adc is still busy converting
GoTo holdAN0 'try again
EndIf
HRSOut3 Dec reading,13 'sendout 0-4095 for 0- 3.3v 12 bits resoloution
Case 200
my_feedbackA.LowByte = tx1[3]
my_feedbackA.HighByte = tx1[4]
my_feedbackB.LowByte = tx2[3]
my_feedbackB.HighByte = tx2[4]
'HRSOut3 Dec my_feedbackA,13
'HRSOut3 Dec my_feedbackB,13
Case 300
my_errorA.LowByte = tx1[5]
my_errorA.HighByte = tx1[6]
my_errorB.LowByte = tx2[5]
my_errorB.HighByte = tx2[6]
'HRSOut3 Dec my_errorA,13
'HRSOut3 Dec my_errorB,13
Case 400
my_current1A.LowByte = tx1[7]
my_current1A.HighByte = tx1[8]
my_current1B.LowByte = tx2[7]
my_current1B.HighByte = tx2[8]
'HRSOut3 Dec my_current1A,13
'HRSOut3 Dec my_current1B,13
Case 500
my_current2A.LowByte = tx1[9]
my_current2A.HighByte = tx1[10]
my_current2B.LowByte = tx2[9]
my_current2B.HighByte = tx2[10]
'HRSOut3 Dec my_current2A,13
'HRSOut3 Dec my_current2B,13,13
sendrecieved = 0
'clear the variables so if rx / tx wires come loose data vanishes
tx1[1] = 0
tx1[2] = 0
tx2[1] = 0
tx2[2] = 0
tx1[3] = 0
tx1[4] = 0
tx2[3] = 0
tx2[4] = 0
tx1[5] = 0
tx1[6] = 0
tx2[5] = 0
tx2[6] = 0
tx1[7] = 0
tx1[8] = 0
tx2[7] = 0
tx2[8] = 0
tx1[9] = 0
tx1[10] = 0
tx2[9] = 0
tx2[10] = 0
sendrecieved = 0
EndSelect 'end of case select
'__________________________________________________________________
'now this is all commented out
'this was used to see how fast 33E can crunch 400 floating point
'calculations took 0.5ms on 33E clocked at 140Mhz
' took 6ms on 18f clocked at 40Mhz
' ball park 12 times faster than 18F
'portg.13 = 1
'For loop = 0 To 100 'do 4 CPU Intensive floating point operations
'a = b * c '1st floating point calcs
'b = a * 1000 '2nd
'c = b / 100000 '3rd
'c = a * 10 '4th
'Next loop
'portg.13 = 0
'_____________________________________________________________________
'loads up the data to be transmitted to the other two mcu's
intout[0]= my_demand.LowByte '1000
intout[1]= my_demand.HighByte
intout[2]= my_feedback.LowByte '2000
intout[3]= my_feedback.HighByte
intout[4]= my_error.LowByte '3000
intout[5]= my_error.HighByte
intout[6]= my_current1.LowByte '4000
intout[7]= my_current1.HighByte
intout[8]= my_current2.LowByte '5000
intout[9]= my_current2.HighByte
intout[10]=$AA 'end of data packet marker $AA = 10101010
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''transmits the 22 bytes of data via charout subroutine ''''''''''''''
T2CONbits_TON = 1' starts 250uS timer 2 which synchronises allthree mcus
'transmits the data
PORTG.13 = 1
For loop = 0 To 10 'tx 22 bytes ,5 words to UTX1 5 words UTX2 + end of packets
GoSub charout 'sub routine to transmit tx1 and tx2 bytes
Next loop1 'repeats the transmit subroutine 10 times
PORTG.13 = 0
hold:
'has valid data been recieved from the other two MCU's
If tx1 [11] = $AA And tx2 [11] = $AA Then
'start 1ms timer
PR1 = 297 'prescallar & 1.0ms interrupt
T1CONbits_TON = 1' 'starts 1ms timer
Else
'no tx1[11] and tx2[11] not equal $AA data not recieved
If flag2 = 1 Then 'has tmr 2 expired
PR1 = 360'360 ' prescalar @ 1.1ms interupt
t1conbits_TON = 1 'start 1ms timer data not recieved
flag2 = 0 '
Else 'otherwise
GoTo hold 'check data has arived next loop until tmr2 expires
EndIf '
EndIf
hold1:
If flag = 1 Then '1ms passed
flag = 0
tx1[11] = 0
tx2[11] = 0
Else
GoTo hold1
EndIf
GoTo Main
'----------------------------------------------------------------------------
charout: 'Fast TX Routine to transmit data to the other 2 Processors
If U1STA.9 = 1 Then 'if U1 Transmit buffer is full wait
GoTo charout:
Else 'otherwise U1 Transmitt buffer is empty send next byte
U1TXREG = intout [loop]
EndIf
holdTX2:
If U2STA.9 = 1 Then 'if U2 Transmitt buffer is full wait
GoTo holdTX2
Else 'otherwise u2 transmitt buffer is empty send next byte
U2TXREG = intout [loop]
EndIf
Return 'exits txout function back to main prog
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''interrupts for individual sources ''''''''''''''''''''
Isr U1RXInterrupt
Inc bufcount
'If bufcount= 11 Then bufcount = 1 'resets counter next packet of 10 bytes arrived
tx1[bufcount] = U1RXREG
If tx1[bufcount] = $AA Then bufcount = 0
Clear IFS0bits_U1RXIF
EndIsr
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Isr U2RXInterrupt
Inc bufcount2
'If bufcount2 = 11 Then bufcount2 = 1
tx2 [bufcount2] = U2RXREG
If tx2[bufcount2] = $AA Then bufcount2 = 0
Clear IFS1bits_U2RXIF
EndIsr
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Isr U3RXInterrupt
REC = U3RXREG
Clear IFS5bits_U3RXIF
EndIsr
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Isr T1Interrupt
'Toggle portg.13
Clear IFS0bits_T1IF
flag = 1
EndIsr
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Isr T2Interrupt
'Toggle portg.13
Clear IFS0bits_T2IF
flag2 = 1
EndIsr
'''''''''''''''not used'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'ISR U1TXInterrupt 'this can be used in future to optimes tx speed
'Clear IFS0bits_u1txif
'u1txreg = rec
'endisr
Next week I will develop this further
I will make it position a large motor with a 4 quadrant H Bridge PCB circuit
More will follow next week etc but for now I need a break for the weekend my brain hurts
Thanks Matty