-
Yes. You definitely need to make RB0/INT0 an input first.
Any time you make a pin an input, then you'll want to hold that pin at logic 1 or 0 through a pull-up or pull-down resistor or some "constant" external logic.
With the hardware PWM output pin connected to the RB0/INT0 input, you could possibly get by without the pull-up or pull-down, but it's cheap insurance that could save you a lot of head-scratching down the road.
A floating input will return random logic values when read, which will cause major problems. Especially with the pin configured as an interrupt source triggered by logic transitions.
If there's any chance that the external pin or device providing the interrupt logic input could be disconnected or go to a high-impedance state, then the pull-up or pull-down resistor is essential.
I always use one. If the interrupt logic is configured to trigger on a high-to-low transition, then I use a pull-up, and let the external signal provide the low-going trigger pulse. If it's setup for low-to-high transitions, then I use a pull-down resistor.
-
What is a good value to use for the pull-down resistor? And if I understand your logic, I shouldn't need the pull-up enable bit (bit 7) on either. So all I should have to do is leave INTCON2 as is and change TRISB to %00000001, right?
-
I normally use 10K or > for pull-up/pull-downs, but pretty much any high value will work.
For INTCON2, you only need to worry about bit 6 INTEDG0: External Interrupt 0 Edge Select bit, and yes, you want to set the pin to an input.
-
So I should bridge pin 17 and PORTB.0 with a 10k resistor? or do I run it to ground? or what?
-
You might also want to consider a series resistor between your PWM output pin & RB0/INT0 *just in case*.
If you're new to all this, the series resistor can keep you from blowing an I/O-pin while tinkering. If you
accidentally make both pins an output, this resistor can save your bacon.
Code:
pull-down
1K
RB0----------|-/\/\/\/\--PWM pin
| 10K pull-down
|--/\/\/\/\----GND
or pull-up
1K
RB0----------|-/\/\/\/\--PWM pin
| 10K pull-up
|--/\/\/\/\----+5V
-
thanks. I'll try that and let you know how it goes.
-
OK, I got a chance to test it today and neither the HWPM nor the +5V trigger to PORTA.2 work. I implemented those changes you suggested and still it doesn't work. Any ideas?
-
can you post your current code?
-
' Connect analog input to (RA0)
' Connect clocks to PORTB
' PORTB.0 is the Reset Clock
' PORTB.1 is the Vphase1 clock
' PORTB.2 is the Vphase2 clock
' PORTB.3 is the Hphase1 clock
' PORTB.4 is the Hphase2 clock
' The reset will be connected to a HWPM clock pin #17, USE FEEDBACK WIRE!!
' Connect pin 2 from the DB9 connector to PORTC.6
' Have a +5V source ready to touch PORTA.2 to trigger clocking process
include "modedefs.bas"
' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
DEFINE OSC 20 ' Sets clock speed to 20Mhz
Define ADC_CLOCK 4 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
HPWM 1,64,3600 ' Supposed to send HPWM 3.6KHZ at 25% duty
INTCON2 = %0100000 ' External Interrupt on Rising Edge
INTCON.7 = 0 ' Disables global interrupts
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001101 ' Set PORTA analog, except for pin 2
TRISB = %00000001 ' Set PORTB to all output, except for bit 0
Pause 500 ' Wait .5 second
Pause 500 ' Wait .5 second
horizpulse var byte
vertpulse var byte
adval var byte ' Create adval to store result
horizpulse = 1 ' Initialize counters, initial states
vertpulse = 1
PORTB.1 = 0
PORTB.2 = 0
PORTB.3 = 1
PORTB.4 = 0
buttonloop:
IF PORTA.2 = 1 then
GOTO vertloop
else
GOTO buttonloop ' Waits for 5V that signals shutter is closed
ENDIF
vertloop:
horizpulse = 1
PORTB.1 = 1
PAUSEUS 139
PORTB.1 = 0
PORTB.2 = 1
PAUSEUS 139
PORTB.1 = 1
PORTB.2 = 0
PAUSEUS 139
PORTB.1 = 0
PAUSEUS 270
vertpulse = vertpulse + 1
IF vertpulse < 512 THEN
GOTO horizloop
ELSE
GOTO buttonloop
ENDIF
horizloop:
INTCON.1 = 0 ' Resets interrupt flag to 0
Resetcheck: ' Loops back to make sure
' the PORTB.3 goes low and
IF INTCON.1 = 0 THEN ' PORTB.4 goes high as close
GOTO Resetcheck ' to the external trigger's
ELSE ' rising edge as possible
ENDIF
PORTB.3 = 0
PORTB.4 = 1
PAUSEUS 139
PORTB.4 = 0
PORTB.3 = 1
PAUSEUS 15
ADCIN 0, adval ' A/D 8 bit value from PORTA.0 into adval
SEROUT2 PORTC.6,16416,[BIN8 adval,13,10] ' Output 8 bit word serially to PC from C.6
PAUSEUS 87
horizpulse = horizpulse + 1
IF horizpulse < 784 THEN
GOTO horizloop
ELSE
PAUSEUS 270
GOTO vertloop
ENDIF
END
-
about this section
Code:
vertpulse = vertpulse + 1
IF vertpulse < 512 THEN
GOTO horizloop
ELSE
GOTO buttonloop
ENDIF
horizloop:
seems to be a common used here to exit from a IF THEN statement... sorry that's a bad practice IMHO and can overflow the stack really fast.
Code:
vertpulse = vertpulse + 1
IF vertpulse >= 512 THEN buttonloop
horizloop:
about this
Code:
IF INTCON.1 = 0 THEN ' PORTB.4 goes high as close
GOTO Resetcheck ' to the external trigger's
ELSE ' rising edge as possible
ENDIF
copy/paste error !?! something missing between ELSE and ENDIF ?!?
For HPWM... looks that you'll have to do it manually... the following is an example for a PIC18F2320
Code:
' PIC18F2320
TRISC.2=0 ' CCP1 AS OUTPUT
Tmr2dutyvar VAR WORD
Tmr2dutyvar=86 ' ((1/3600)/4)/20Mhz / 16(prescaller)
PR2=$57 ' load frequency period (PWM freq=3.6 KHZ)
'set Duty cycle
CCP1CON.5=Tmr2dutyvar.1
CCP1CON.4=Tmr2dutyvar.0
CCPR1L =(tmr2dutyvar>>2)
T2CON=%00000110 ' start timer 2
' set prescaler = 16
CCP1CON = %00001100 ' pwm mode for CCP1
HERE: GOTO HERE
-
Is there something wrong with the HWPM command that I used? And I suppose if I take out the ELSE statement in the reset check loop, and go straight to ENDIF, then that would fix a logical error. Is my ADCON1 register set correctly to receive a digital input on PORTA.2? Also, using your clock example, would that program ever advance past the "HERE" line? The whole idea of doing the HWPM was so it would run in the background while the loops were dependant on certain conditions. thanks for the help.
-
yes for sure Here: goto here can be skip. just for testing purpose on a single task.
once you start the PWM it will run as long as you have power on the PIC.. DEUH!!!
For the HPWM statement... i always set register myself. Never tested this one. By the book it's suppose to work... or i'm missing something like a DEFINE something stuff.
-
Alright, I'll try that tomorrow and let you know how it goes. thanks again.
-
OK, I tried it and I am getting the same problem. CCP1 does not appear to be oscillating at all. Also, do I have to set PORTA.2 to receive an external interrupt?? I thought if I just test and see if it has a 5V source on it, that would suffice, but maybe not. I just plopped your code example in there to see if it would compile, and it did, but does not appear to generate the desired function.
' Connect analog input to (RA0)
' Connect clocks to PORTB
' PORTB.0 is the Reset Clock
' PORTB.1 is the Vphase1 clock
' PORTB.2 is the Vphase2 clock
' PORTB.3 is the Hphase1 clock
' PORTB.4 is the Hphase2 clock
' The reset will be connected to a HWPM clock pin #17, USE FEEDBACK WIRE!!
' Connect pin 2 from the DB9 connector to PORTC.6
' Have a +5V source ready to touch PORTA.2 to trigger clocking process
include "modedefs.bas"
' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
DEFINE OSC 20 ' Sets clock speed to 20Mhz
Define ADC_CLOCK 4 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
' PIC18F2320 example, may need to change
TRISC.2=0 ' CCP1 AS OUTPUT
Tmr2dutyvar VAR WORD
Tmr2dutyvar=86 ' ((1/3600)/4)/20Mhz / 16(prescaller)
PR2=$57 ' load frequency period (PWM freq=3.6 KHZ)
'set Duty cycle
CCP1CON.5=Tmr2dutyvar.1
CCP1CON.4=Tmr2dutyvar.0
CCPR1L =(tmr2dutyvar>>2)
T2CON=%00000110 ' start timer 2
' set prescaler = 16
CCP1CON = %00001100 ' pwm mode for CCP1
INTCON2 = %0100000 ' External Interrupt on Rising Edge
INTCON.7 = 0 ' Disables global interrupts
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001101 ' Set PORTA analog, except for pin 2
TRISB = %00000001 ' Set PORTB to all output, except for bit 0
Pause 500 ' Wait .5 second
Pause 500 ' Wait .5 second
horizpulse var byte
vertpulse var byte
adval var byte ' Create adval to store result
horizpulse = 1 ' Initialize counters, initial states
vertpulse = 1
PORTB.1 = 0
PORTB.2 = 0
PORTB.3 = 1
PORTB.4 = 0
buttonloop:
IF PORTA.2 = 1 then
GOTO vertloop
else
GOTO buttonloop ' Waits for 5V that signals shutter is closed
ENDIF
vertloop:
horizpulse = 1
PORTB.1 = 1
PAUSEUS 139
PORTB.1 = 0
PORTB.2 = 1
PAUSEUS 139
PORTB.1 = 1
PORTB.2 = 0
PAUSEUS 139
PORTB.1 = 0
PAUSEUS 270
vertpulse = vertpulse + 1
IF vertpulse < 512 THEN
GOTO horizloop
ELSE
GOTO buttonloop
ENDIF
horizloop:
INTCON.1 = 0 ' Resets interrupt flag to 0
Resetcheck: ' Loops back to make sure
' the PORTB.3 goes low and
IF INTCON.1 = 0 THEN ' PORTB.4 goes high as close
GOTO Resetcheck ' to the external trigger's
' rising edge as possible
ENDIF
PORTB.3 = 0
PORTB.4 = 1
PAUSEUS 139
PORTB.4 = 0
PORTB.3 = 1
PAUSEUS 15
ADCIN 0, adval ' A/D 8 bit value from PORTA.0 into adval
SEROUT2 PORTC.6,16416,[BIN8 adval,13,10] ' Output 8 bit word serially to PC from C.6
PAUSEUS 87
horizpulse = horizpulse + 1
IF horizpulse < 784 THEN
GOTO horizloop
ELSE
PAUSEUS 270
GOTO vertloop
ENDIF
END
-
mm, unfortunately i don't have any of those 4520 here to test... weird. - Did you set config fuse to HS oscillator in your programmer?!?
- is your MCLR is tie to VCC via 1K-10K resistor ?!?
- is your supply line 5volts and clean... have you place 0.1 uF close to the PIC?!?
- Is a stupid blink light on this pin work?!?
-
Yes, I have HS configured in the EPIC programmer.
My master clear is tied to +5V through a 1k resistor.
My supply line is 5V.
And I was doing a lot more than the blinking program on this PIC before I tried doing the HPWM.
-
It wouldn't have anything to do with my DEFINE ADC_CLOCK line, would it?
-
When you're having problems like this, it's sometimes much easier to simplify
everything & start with a single peice of code that gets a peripheral you're
having problems with working first.
Then move on to testing your other routines step-by-step, inserting tested
& known working code with new code until you hit the problem again. Then
you at least have some idea of what may be causing the problem.
Try something like this without any other code to test your CCP module.
Disconnect all external circuits just in case it's an external circuit connection
that causing problems.
Code:
DEFINE HPWM1_TIMER 2
DEFINE CCP1_REG PORTC
DEFINE CCP1_BIT 2 ' RC2
DEFINE HPWM2_TIMER 2
DEFINE CCP2_REG PORTC
DEFINE CCP2_BIT 1 ' RC1
TRISC.1 = 0
TRISC.2 = 0 ' HPWM automatically does this for you, but just in case
CCP1CON = %00001100 ' PWM mode
CCP2CON = %00001100 ' PWM mode
HPWM 1,127,3600 ' RC2/CCP1 3.6kHz @ ~50% duty
HPWM 2,127,3600 ' RC1/CCP2 3.6kHz @ ~50% duty
Does it work?
If it does, then you have verified your CCP module & PBP code to control
it. If it doesn't work then you may have a hardware or config problem.
Your PBP device header file (18F4520.INC) has "deafult" config fuse settings
in it something like the following;
Code:
INCLUDE "P18F4520.INC" ; MPASM Header
__CONFIG _CONFIG1H, _OSC_XT_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
If you think maybe your programmer isn't setting HS when selected manually,
then edit your inc file changing XT in the first line to HS. Save it, close it,
and re-compile. Then check to see if it's really set to HS when you open the
.HEX file with your programmer before programming your 4520.
Once you get hardware PWM working, then move on to insert other code
modules one-by-one, and re-test.
-
Thanks again for your input. I will definitely try that and let you guys know what happens.
Oh, and I checked on the .INC file and the folder with this software doesn't come with any of the 18 series include files. I guess that could be a major problem. I am looking in the PBP/INC directory in case I should be looking somewhere else.
-
OK, I tried your HPWM test code and I do get a clock, however the Oscope says it is running at 18.15kHz, not 3.6kHz. Any ideas??
-
OK, now this is weird.
I put the exact same code into my program and now it reads 3.6kHz. In addition, it only reads correctly if I don't have those resistors between RB0 and CCP1. When they are in, the frequency goes to 0. Am I overloading the timer or something? Anyway, I am going to try and see if I can take out those resistors and see if my program will run with the PORTA.2 touching the 5V. thanks again.
-
OK, it seems to work fine when I don't have the CCP1 pin connected to the RB0 pin, which I guess means that I have my interrupt somehow coded wrong. I thought that I had the TRISB register set correctly to all outputs except for PORTB.0
-
mmm, are you saying PWM stops when you jump CCP1 to RB0? what about if you do a simple blink on this RB0 pin... or do a simple IF THEN on RB0
IF PORTB.0 = 1 then DoSomething or Start HPWM
maybe this pin is burn now :(
-
So you are saying that my code is right and the pin is just acting weird? If it is burned, I have like 8 more PIC chips. I just want to make sure that the problem is fixed before I burn another one. So I will test this PORTB.0 with a simple blink program to see if it responds at all. I'll let you know what happens. Thanks again for your continued input.
-
Do the RB0 test before using others.
BTW, i'm about setting up a board to do something like this for testing. ì'll post the code here.
-
This one use RB0 interrupt handler routine.
Code:
' INT0 with PWM
' =============
'
' File name : INT0_PWM.bas
' Company : Mister E
' Programmer : Steve Monfette
' Date : 02/03/2005
' Device : PIC18F2320
'
'
' This program generate 3.6Khz @ 25% duty PWM on CCP1 pin.
'
' CCP1 pin is connected to RB0 pin via 1k resistor.
'
' Program blink a led on PORTB.6 while generating PWM.
' Each rising edge of PWM, PORTB.7 is toggle. This will
' generate 1.8Khz 50% duty on this PORTB.7 pin.
'
' We will use Interrupt on RB0(INT0) pin to detect rising edge
' Hardware Definition
' ===================
'
DEFINE LOADER_USED 1
DEFINE OSC 20
TRISB=%00000001 ' PORTB.0 as input
TRISC.2=0 ' PORTC as output
ADCON1=$0F ' disable A/D converter
' Interrupt definition
' ====================
'
INTCON=%10010000 ' Enable global interrupt
' Enable INT0 interrupt (RB0)
INTCON2.6=1 ' Enable INT0 on rising edge
ON interrupt goto RB0_interrupt
' Variable definition
' ===================
'
Tmr2dutyvar VAR WORD
DelayLoop var word
' PWM signal generation
' =====================
'
Tmr2dutyvar=86 ' load duty time duration
PR2=$57 ' load frequency period (PWM freq=3.6 KHZ)
' set Duty cycle
' --------------
'
CCP1CON.5=Tmr2dutyvar.1
CCP1CON.4=Tmr2dutyvar.0
CCPR1L =(tmr2dutyvar>>2)
T2CON=%00000110 ' start timer 2
' set prescaler = 16
CCP1CON = %00001100 ' pwm mode for CCP1
' Main Loop
' =========
'
' Stupid led blink on PORTB.6...
'
Start:
toggle PORTB.6
for delayloop = 1 to 50000
pauseus 5
next
goto start
' Interrupt routine
' =================
'
' toggle PORTB.6 each rising edge on it.
'
disable
RB0_interrupt:
toggle PORTB.7
INTCON.1=0 ' reset INT0 interrupt flag
resume
enable
-
By polling INT0 flag bit... no interrupt handler
Code:
' INT0 polling with PWM
' =====================
'
' File name : INT0_poll_PWM.bas
' Company : Mister E
' Programmer : Steve Monfette
' Date : 02/03/2005
' Device : PIC18F2320
'
'
' This program generate 3.6Khz @ 25% duty PWM on CCP1 pin.
'
' CCP1 pin is connected to RB0 pin via 1k resistor.
'
' Program blink a led on PORTB.6 while generating PWM.
' Each rising edge of PWM, PORTB.7 is toggle. This will
' generate 1.8Khz 50% duty on this PORTB.7 pin.
'
' We will check INTCON.1 (Interrupt on RB0(INT0) flag bit) pin to
' detect rising edge
' Hardware Definition
' ===================
'
DEFINE LOADER_USED 1
DEFINE OSC 20
TRISB=%00000001 ' PORTB.0 as input
TRISC.2=0 ' PORTC as output
ADCON1=$0F ' disable A/D converter
' Interrupt definition
' ====================
'
INTCON=%00010000 ' Disable global interrupts
' Enable INT0 interrupt (RB0)
INTCON2.6=1 ' Enable INT0 on rising edge
' Variable definition
' ===================
'
Tmr2dutyvar VAR WORD
DelayLoop var word
' PWM signal generation
' =====================
'
Tmr2dutyvar=86 ' load duty time duration
PR2=$57 ' load frequency period (PWM freq=3.6 KHZ)
' set Duty cycle
' --------------
'
CCP1CON.5=Tmr2dutyvar.1
CCP1CON.4=Tmr2dutyvar.0
CCPR1L =(tmr2dutyvar>>2)
T2CON=%00000110 ' start timer 2
' set prescaler = 16
CCP1CON = %00001100 ' pwm mode for CCP1
' Main Loop
' =========
'
' Stupid led blink on PORTB.6 while testing INT0 flag bit
'
Start:
toggle PORTB.6
for delayloop = 1 to 50000
pauseus 5
if INTCON.1 then
INTCON.1=0
toggle PORTB.7
endif
next
goto start
-
Is there any reason why I can't get the process to start when PORTA.2 goes high?? I haven't rewired the HPWM/RB0 yet, but that should not matter. The PIC should at least shift the vertical clocks before even checking the reset (interrupt flag). Are my registers set correctly for analog input to PORTA.0 and digital input to PORTA.2??
-
to have analog on PORTA.0
ADCON1=%00001110
-
OK, well I think I got it working so that it waits for the push button to start, but then it seems like it keeps going even after the loops have completed. I will put in an LED command to signal when clocking is in progress and see what happens. It may just be that my calculations for transfer times are just off. I figured that at 3.6Khz sampling (which is how fast we would be sampling the real data), we could sample the data, do the A/D, and send the data off chip in about 2 minutes at 19200 inverted baud. Am I off in my estimations?
-
Again, I have about 350,000 samples to take and transfer off the PIC serially.
-
OK, I think I finally have everything working. I have an LED on bit 5 of portb the is low when the clocks are not running and hig when they are. I had had a problem with the switch on PORTA.2 floating high, so I put a 2 position switch between 5v and ground and that seemed to solve the problems. My next move is to hook up a logic analyzer to make sure all the clocks are synched up. Thanks guys for all your help and I'll let you know how it goes.
-
you don't really need that kind of switch.... place a pull-up or pull down resistor will do the job as well.
For the logic analyser stuff... great idea if you have one. Case not, if you still have an analog scope, it's easy to build one with few CMOS components.
-
We have a DigiView in the lab. I have never used one, so I am gonna take a few hours one day just to learn the ropes. But yeah, I definitely have one to work with. Also, since this is a camera, I think a physical button was the way to go, since, down the road, we want to make this thing hand held.
-
OK, I think I realized why my data is taking so long to transfer. It is because the PIC is sending the character representations of 1's and 0's, which are 49's and 48's. Is there a way to send out true binary data? (Matlab picks up the 48's and 49's which is how i figured the whole thing out.) thanks.
-
OK, I have increased the baud rate to 38,400 baud. I have exactly 407,680 8 bit samples to take. I am sampling at about 3.6kHz. How long do you think it should take to sample the data, do the A/D conversion, and send it off chip serially at these speeds?
Here are my time calculations:
407680 samples = 3261440 true bits of data. However, since the PIC can only send out the ASCII representation of the formats I want (mainly decimal and binary), each number (the 2 in 255 for example) requires 5 bits to store since the decimal value of the character '2' is 50. So technically, each bit of data sampled on the PIC requires 5 bits to transfer. So I estimated that since there are 5 bits to send for each bit sampled, the total amount of data to transmit is 16,307,200 bits. So, if the data were already on chip, it would take about 8 minutes to get it all out. Then I added the amount of time it takes to sample the data, which is about 3 minutes (with slack) and so the whole thing should take around 10 minutes, right? Wrong, hyperterminal has been reading data for over a half hour and is still going. Should I use a faster sampling rate since I can now send data out at 38,400 baud? Am I overloading the stack? I don't know.
thanks for any input.
-
OK, we got the logic analyzer working and I think some of the commands are taking a lot longer than I had expected.
The horizontals are not even close to 50% duty cycle as one is high for 960 usec and low for 140usec (the other horizontal is opposite). I was shooting for more in the neighborhood of 140usec for both the high and low for both clocks. In addition, I can't seem to see the vertical on the logic analyzer at all. Maybe i need to adjust some settings, but I'm gettin kinda worried.
thanks for all the help.
My updated code is below:
' Connect analog input to (RA0)
' Connect clocks to PORTB
' PORTB.0 is the Reset Clock
' PORTB.1 is the Vphase1 clock
' PORTB.2 is the Vphase2 clock
' PORTB.3 is the Hphase1 clock
' PORTB.4 is the Hphase2 clock
' The reset will be connected to a HWPM clock pin #17, USE FEEDBACK WIRE!!
' Connect pin 2 from the DB9 connector to PORTC.6
' Have a +5V source ready to touch PORTA.2 to trigger clocking process
include "modedefs.bas"
' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
DEFINE OSC 20 ' Sets clock speed to 20Mhz
' Define HPWM parameters
DEFINE HPWM1_TIMER 2
DEFINE CCP1_REG PORTC
DEFINE CCP1_BIT 2 ' RC2
TRISC.1 = 0 ' HPWM automatically does this for you, but just in case
CCP1CON = %00001100 ' PWM mode
HPWM 1,127,3600 ' RC2/CCP1 3.6kHz @ ~50% duty
' Define interrupt parameters
INTCON2 = %01000000 ' External Interrupt on Rising Edge
INTCON.7 = 0 ' Disables global interrupts
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001110 ' Set PORTA digital, except for bit 0
TRISB = %00000001 ' Set PORTB to all output, except for bit 0
Pause 500 ' Wait .5 second
Pause 500 ' Wait .5 second
horizpulse var byte
vertpulse var byte
adval var byte ' Create adval to store result
horizpulse = 1 ' Initialize counters, initial states
vertpulse = 1
PORTB.1 = 0
PORTB.2 = 0
PORTB.3 = 1
PORTB.4 = 0
PORTB.5 = 0 ' LED off indicates that the camera is not transferring data
buttonloop:
IF PORTA.2 = 1 then
PORTB.5 = 1
GOTO vertloop
else
PORTB.5 = 0
GOTO buttonloop ' Waits for 5V that signals shutter is closed
ENDIF
vertloop:
horizpulse = 1
PORTB.1 = 1
PAUSEUS 139
PORTB.1 = 0
PORTB.2 = 1
PAUSEUS 139
PORTB.1 = 1
PORTB.2 = 0
PAUSEUS 139
PORTB.1 = 0
PAUSEUS 270
vertpulse = vertpulse + 1
IF vertpulse < 512 THEN
GOTO horizloop
ELSE
PORTB.5 = 0
GOTO buttonloop
ENDIF
horizloop:
INTCON.1 = 0 ' Resets interrupt flag to 0
Resetcheck: ' Loops back to make sure the PORTB.3 goes low and
IF INTCON.1 = 0 THEN ' PORTB.4 goes high as close
GOTO Resetcheck ' to the external trigger's rising edge as possible
ENDIF
PORTB.3 = 0
PORTB.4 = 1
PAUSEUS 139
PORTB.4 = 0
PORTB.3 = 1
PAUSEUS 15
ADCIN 0, adval ' A/D 8 bit value from PORTA.0 into adval
SEROUT2 PORTC.6,16390,[DEC adval] ' Output 8 bit word serially to PC from C.6
PAUSEUS 87
horizpulse = horizpulse + 1
IF horizpulse < 784 THEN
GOTO horizloop
ELSE
PAUSEUS 270
GOTO vertloop
ENDIF
END
-
OK, so I commented out the serout2 line and now the high time is 140 usec and the low is 420usec. I think this means that the serout2 command line is taking 540usec to execute for some reason. Is there any reason for this?? Can I speed it up??
After i did that test, I commented out the pauseus 15 and pauseus 87 in addition to the serout2 and it was almost perfect high 150 usec and low 140 usec. The weird part is that i have pauses for the high, but none for the low and they are still in synch. So, yeah basically the serout2 command is taking for ever.
-
use HSEROUT or access direct to the internal USART registers. SERIN/SEROUT and others are good when your PIC don't have any internal USART OR when you want to use multiples serial i/o's
@20MHZ you'll be able to use higher baudrate than with the PBP statements.
-
Well it's not the baud rate that i slowing it down (or atleast not as much), it's the time it takes to run through the command. I suppose I could just see if I could slow the clocks down so that I could still maintain 50% duty cycle. Also, how fast do the HSEROUT commands take to execute? because the baudrate will not make much of a difference if I run into the same issue of execution time as I do with the SEROUT2 command.