I need to drive a SG90 micro servo.
On a pic I set up a sub, that was called by an interrupt
DimTIME_BASEaliasCCPR1H,CCPR1LASWORD'// Programmable "Compare" ValueCCP1CON=b'00001011''// Config CCP1 for Compare:Spcl Event trigger;TIME_BASE=20000'// Make period timer = 20ms (2000us);OnInterruptCCP1CallServo_Pulse;every 20ms;InitTimer1OSC,PS1_4'// Configure timer1 with Prescale of 1:4ClearTimer1'//Make Sure timer is cleared to 0StartTimer1'// Start the timer
from memory it worked.
The mega328p timer demos saying 50Hz are KHz.
Please,how to get a 50 Hz interrupt sub on mega 328p.
I want to use a nano and slc,sda is reversed compared to uno. Are there other differences between
them as far as interrupts?
Updating servos every 20 ms is normal. I thought it was so they don't forget their position but might be to allow for mechanical movement between pulses.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have tried various things and timer calculators to no avail scoping a sub when it worked that turned a port on/off and measuring frequency.
I have checked code by Kent but I could not adapt it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
thesevaluesfromtimercalculatorfor1000msIneedvaluesfor20ms
#chipmega328p,16
#optionexplicit
#defineLEDPORTB.4dirLEDout' Add the handler for the interruptOnInterruptTimer1Match1CallInterruptHandler' Initialise the timer - this is required' Set prescaler and then start the timerInitTimer1Osc, PS_1_1' Start the timer - this is requiredStartTimer1TCCR1A=0x80;TCCR1B=0x0C;OCR1AH=0xF4;OCR1AL=0x23;'Main routineDoLoopend'This will be called when the Timer matchesSubInterruptHandlerLED=!LEDEndSub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I searched the forum for interrupts and found one I wrote that is not in my files.
It works on a nano as well it seems now.
I must have erased the file but only remember the pic version..age?
Would be useful in demos.
; A uno servo controller that refreshes the servo every 20ms; servopos is the servo position
#chipmega328p,16
#configosc=int
#optionExplicit;dimcountasworddimservoposasworddimservodirasbyte
#defineservopinportb.0;uno pin 8servopos=1500;usservodir=1dirservopinout;TCCR1A=0x80;TCCR1B=0x0A;OCR1AH=0x9c;OCR1AL=0x3f;OnInterruptTimer1Match1callISR;main programdo;ifservodir=1thenservopos=servopos+80;usifservopos=2220then;end of servo turnservodir=0endifelseservopos=servopos-80;usifservopos=780then;end of servo turnservodir=1endifendif;wait100msloop;end main programsubISRpulseoutservopin,servoposusendsub
Last edit: stan cartwright 2020-05-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
then the interrupt does not happen.Comment out and the interrupt works ok.
I don't see the connection.
; A uno servo controller that refreshes the servo every 20ms; servopos is the servo position
#chipmega328p,16
#configosc=int
#optionExplicit;;set up v53l0x
#defineHI2C_DATAPORTC.4;sda
#defineHI2C_CLOCKPORTC.5;scl
#defineHI2C_BAUD_RATE400HI2CModeMaster;;v53l0x software restart'HI2CStart'HI2CSend(0x52)'HI2CSend(0x89)'HI2CSend(0x01)'HI2CStop'wait 200 ms;dimservoposasworddimservodir,distance_lo,distance_hiasByte
#definedistancedistance_hi*256+distance_lo
#defineservopinportb.0;uno pin 8servopos=1500;usservodir=1dirservopinout;TCCR1A=0x80;TCCR1B=0x0A;OCR1AH=0x9c;OCR1AL=0x3f;OnInterruptTimer1Match1callISR;main programdo;ifservodir=1thenservopos=servopos+80;usifservopos=2220then;end of servo turnservodir=0endifelseservopos=servopos-80;usifservopos=780then;end of servo turnservodir=1endifendif;wait20msloop;end main programsubISRpulseoutservopin,servoposusendsub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It seems, for mega328p that On Interrupt Timer1Match1 call ISR and hi2c don't work together.
Er..this is a problem?...does my head.
Please look at this. I'll see if it is the same with pics.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am reinstalling gcb. It is behaving odd.
This prog works now.
I started with v53l0x to terminal then added interrupt.
the servo is going back fwd from an intrrupt and there's a distance from v53l0x to terminal.
This wasn't happening before and the code looks the same.I just started with working code and added until not working. This code seems no different but works. Runs the servo and reads v53l0x.
#chipmega328p, 16
#optionExplicit;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;set up v53l0x
#defineHI2C_DATAPORTC.4;sda portc.4 for nano portc.5 for uno
#defineHI2C_CLOCKPORTC.5;scl portc.5 for nano portc.4 for uno
#defineHI2C_BAUD_RATE400HI2CModeMaster;servo 50 Hz subTCCR1A=0x80;TCCR1B=0x0A;OCR1AH=0x9c;OCR1AL=0x3f;OnInterruptTimer1Match1callISR;dimservoposasworddimservodir,distance_lo,distance_hiasByte
#definedistancedistance_hi*256+distance_lo
#defineservopinportb.0;uno pin 8dirservopinoutservopos=1500;usservodir=1HI2CStart;software restartHI2CSend(0x52)HI2CSend(0x89)HI2CSend(0x01)HI2CStopwait200msdo;------------------------HI2CStart;Sys Range StartHI2CSend(0x52)HI2CSend(0x00)HI2CSend(0x01)HI2CStopHI2CStart;read distanceHI2CSend(0x52)HI2CSend(0x1e)HI2CReStartHI2CSend(0x53);set the read flagHI2CReceive(distance_hi)HI2CReceive(distance_lo, NACK);read one byte and concludeHI2CStophserprint" Distance: "+str(distance_hi*256+distance_lo)+" mm ";;move servo left rightifservodir=1thenservopos=servopos+80;usifservopos=2220then;end of servo turnservodir=0endifelseservopos=servopos-80;usifservopos=780then;end of servo turnservodir=1endifendif; pulseout servopin,servopos us ;move servo;wait50msloopsubISRpulseoutservopin,servoposusendsub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Reinstalled gcb...it took up the main windoe,had to minimise it to see compile reports and programmer preferences.back to normal and this program works. The v53l0x hi2c and the servo interrupt.Looks the same as before...more or less. I just built it from working v53l0x to terminal,added the interrupt and it still worked.
added code for object detection...surprised how short the code was.
;v53l0x on servo robot
#chipmega328p,16
#optionexplicitdirportc.0,portc.1,portc.2,portc.3,portb.0out
#defineservopinportb.0
#definelmotfwdsetportc.0off : setportc.1on
#definelmotrevsetportc.0on : setportc.1off
#definelmotstopsetportc.0off : setportc.1off
#definermotfwdsetportc.2off : setportc.3on
#definermotrevsetportc.2on : setportc.3off
#definermotstopsetportc.2off : setportc.3off;set up v53l0x
#defineHI2C_DATAPORTC.4;sda portc.4 for nano portc.5 for uno
#defineHI2C_CLOCKPORTC.5;scl portc.5 for nano portc.4 for uno
#defineHI2C_BAUD_RATE400HI2CModeMaster;dimservoposasworddimservodir,distance_lo,distance_hiasByte
#definedistancedistance_hi*256+distance_loservopos=1500:servodir=1;servo interruptTCCR1A=0x80;TCCR1B=0x0A;OCR1AH=0x9c;OCR1AL=0x3f;OnInterruptTimer1Match1callisr;v53l0x software restartHI2CStartHI2CSend(0x52)HI2CSend(0x89)HI2CSend(0x01)HI2CStopwait200ms;lmotfwd : rmotfwd;go forwarddo;------------;is object in range?getdistanceifdistance<150then;object closelmotstop : rmotstopifservopos<1500then;is object to leftdolmotfwd : rmotrevwait50mslmotstop : rmotstopgetdistanceloopuntildistance>150;rotate right until clearelsedo;object rightlmotrev : rmotfwdwait50mslmotstop : rmotstopgetdistanceloopuntildistance>150;rotate left until clearendifservopos=1500;point servo forwardlmotfwd : rmotfwd;go forwardendif;rotate radar Left-rightifservodir=1thenservopos=servopos+80ifservopos=2220thenservodir=0endifelseservopos=servopos-80ifservopos=780thenservodir=1endifendifloop;-------------;subgetdistanceHI2CStart;Sys Range StartHI2CSend(0x52)HI2CSend(0x00)HI2CSend(0x01)HI2CStopHI2CStart;read distanceHI2CSend(0x52)HI2CSend(0x1e)HI2CReStartHI2CSend(0x53);set the read flagHI2CReceive(distance_hi)HI2CReceive(distance_lo, NACK);read one byte and concludeHI2CStopendsubsubisrpulseoutservopin,servoposusendsub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
in a program and not having the device connected makes it hang..I don't know.
If not for lockdown I wouldn't have had the time to find this out.
I built the program several times and it didn't work then it finally did.
It worked after I connected a vl53l0x so WAS that the problem?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Why does
TCCR1A = 0x80;
TCCR1B = 0x0A;
OCR1AH = 0x9c;
OCR1AL = 0x3f;
On Interrupt Timer1Match1 call isr
make ili9341 crash?
The 50Hz interrupt works but not with ili9341..screen goes white.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I need to drive a SG90 micro servo.
On a pic I set up a sub, that was called by an interrupt
which called
from memory it worked.
The mega328p timer demos saying 50Hz are KHz.
Please,how to get a 50 Hz interrupt sub on mega 328p.
I want to use a nano and slc,sda is reversed compared to uno. Are there other differences between
them as far as interrupts?
Updating servos every 20 ms is normal. I thought it was so they don't forget their position but might be to allow for mechanical movement between pulses.
I have tried various things and timer calculators to no avail scoping a sub when it worked that turned a port on/off and measuring frequency.
I have checked code by Kent but I could not adapt it.
I posted this for 1000ms
I searched the forum for interrupts and found one I wrote that is not in my files.
It works on a nano as well it seems now.
I must have erased the file but only remember the pic version..age?
Would be useful in demos.
Last edit: stan cartwright 2020-05-29
Now here is probably the problem I had.
The interrupt works until I add
then the interrupt does not happen.Comment out and the interrupt works ok.
I don't see the connection.
It seems, for mega328p that On Interrupt Timer1Match1 call ISR and hi2c don't work together.
Er..this is a problem?...does my head.
Please look at this. I'll see if it is the same with pics.
I am reinstalling gcb. It is behaving odd.
This prog works now.
I started with v53l0x to terminal then added interrupt.
the servo is going back fwd from an intrrupt and there's a distance from v53l0x to terminal.
This wasn't happening before and the code looks the same.I just started with working code and added until not working. This code seems no different but works. Runs the servo and reads v53l0x.
Reinstalled gcb...it took up the main windoe,had to minimise it to see compile reports and programmer preferences.back to normal and this program works. The v53l0x hi2c and the servo interrupt.Looks the same as before...more or less. I just built it from working v53l0x to terminal,added the interrupt and it still worked.
added code for object detection...surprised how short the code was.
I am guessing that using
in a program and not having the device connected makes it hang..I don't know.
If not for lockdown I wouldn't have had the time to find this out.
I built the program several times and it didn't work then it finally did.
It worked after I connected a vl53l0x so WAS that the problem?
Look at the HWI2C code, or, the Help. There is a variable you can check the will return the state of the address from memory
HI2CAckPollState
Why does
TCCR1A = 0x80;
TCCR1B = 0x0A;
OCR1AH = 0x9c;
OCR1AL = 0x3f;
On Interrupt Timer1Match1 call isr
make ili9341 crash?
The 50Hz interrupt works but not with ili9341..screen goes white.