This example code demonstrates how a PIC with a Programmable Ramp Generator (PRG) peripheral can produce a sawtooth signal and route the output to an I/O pin. A PIC 16F1769 was used, but any PIC with a PRG peripheral should work in a similar manner.
PIC chips with Core Independent Peripherals (CIPS) allow tasks to be done with out taking up valuble core resources. In this example the following CIPS are used. Timer2, DAC1, OPA0, OPA1, PWM3 & PRG0. No external components are required.
The code below will generate a 38KHz sawtooth signal on Pin 7. The signal is referenced to 0V via the DAC a DAC level of 0. The rate of rise is .2V/us . The peak amplitude is 4.2V
By adjusting PWM, DAC Level, PRG Mode & rise/fall rates, a sawtooth, falling ramp, or triangle signal can be generated at various frequencies and rising/falling rates.
Here is the code to get started. I used a 4x 20 LCD display for code development/debugging. The Printbits sub displays a register in binary format. Example: Printbits PRG1CON0,0. This will print the register bits on line 0 of the LCD Display.
'''A demonstration program for GCB.'''---------------------------------------------------'''Program to Demo Programable Ramp Generater'''WilliamRoth'''12.01.2016'''****************************************************#chip16F1769,32#configOSC=INTOSC,MCLRE=ON,WDTE=OFF,PPS1WAY=OFF#OPTIONExplicit'''Using SWI2C for Display #define I2C_MODE Master #define I2C_DATA PORTB.4 #define I2C_CLOCK PORTB.6 #define I2C_DISABLE_INTERRUPTS ON #define LCD_IO 10 #define LCD_I2C_Address_1 0x4E #define FOSC 1 #define FOSC4 2 CLS PRINT "Great Cow Basic" Locate 1,0 Print "PRG Demo" Locate 2,0 Print ChipNameStr wait 1 SINIT: INIT_PWM3 ;Setup PWM3 START_PWM3 ;Start the PWM INIT_DAC1 ;Setup DAC1 INIT_OPA1 ;Setup OPAMP1 INIT_OPA2 ;Setup OPAMP2 INIT_PRG1 ;Setup PRG1MAIN: START_PRG1 ; GO!'---- Main loop ----Do'Main program goes hereLOOP'------- Sub Routines ----------SubINIT_DAC1DAC1CON0=b'00000000';DefaultsDAC1REFH=b'00000000';DAC1REFL=b'00000000';SETDAC1CON0.5OFF;DAC1isonNOOutputonRA0SETDAC1CON0.7ON;ENABLEDAC1SETDAC2LDON;WriteDACValtoBuffersEndSubSubINIT_OPA1OPA1PCHS=b'00000010';POSINputisDAC1OUTOPA1CON=b'10010000';EnablewithUnityGain(Buffer)EndSubSubINIT_OPA2OPA2PCHS=b'00001000';POSIN=PRG1OUTOPA2CON=b'10010000';EnablewithUnityGain;PRGOutputwillbeonRC3Pin7EndSubSubINIT_PRG1;SettingupRampGeneratorasRisingRamp;DAC1/OPA1asReferenceSupplysource;PWM3astimingsourceSETPRG1CON0.7ON'Enable Module Do while PRG1CON1.2 = 0 'waitforPRGReadyLoopSETPRG1CON0.3ON;Mode=RisingSETPRG2CON0.2OFFSETPRG1CON0.5ON;EdgeTriggeredSETPRG1CON0.4ONSETPRG1CON0.1ON;Oneshotenabled(SeeDatasheet)SETPRG1CON1.1ON;SetPolaritySETPRG1CON1.0OFF;SetPolarityPRG1CON2=b'00000000';Sloperatedefault.2us/vPRG1FTSS=b'00000101';FallingTimingSourcePWM3PRG1RTSS=b'00000101';RisingTimingsourcePWM3EndSubSubSTART_PRG1SetPRG1CON0.0ON;GO!StartthePRGEndSubSubSTART_PWM3SetTMR2IFOFF;ClearthebitSetT2CON.7ON;StartthetimerPWM3CON=b'10000000';StartthePWMEndSubSubStop_PWM3PWM3CON=b'00100000';StopthePWMSetTMR2IFOFF;ClearthebitSetT2CON.7OFF;StartthetimerTMR2=0EndSubSubINIT_PWM3T2PR=200;Defaultis255atPORT2CLKCON=FOSC4T2CON=b'00100000';SetTMR2PrescalePWM3CON=0;ClearPWM3DCH=175;HighbitsPWM3DCL=0;LowBitsEndSubSubprintbits(databyte,LCD_line)LocateLCD_line,0Print" "'clearthelinelocateLCD_line,0Printdatabyte.7:Print" "Printdatabyte.6:Print" "Printdatabyte.5:Print" "Printdatabyte.4:Print" "Printdatabyte.3:Print" "Printdatabyte.2:Print" "Printdatabyte.1:Print" "Printdatabyte.0:Print" "endsubend
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Could be useful to control x/y scope and bresenham's line algo to do vector graphics like asteroids was , 2 ramp generators clocked at different frequency.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This example code demonstrates how a PIC with a Programmable Ramp Generator (PRG) peripheral can produce a sawtooth signal and route the output to an I/O pin. A PIC 16F1769 was used, but any PIC with a PRG peripheral should work in a similar manner.
PIC chips with Core Independent Peripherals (CIPS) allow tasks to be done with out taking up valuble core resources. In this example the following CIPS are used. Timer2, DAC1, OPA0, OPA1, PWM3 & PRG0. No external components are required.
The code below will generate a 38KHz sawtooth signal on Pin 7. The signal is referenced to 0V via the DAC a DAC level of 0. The rate of rise is .2V/us . The peak amplitude is 4.2V
By adjusting PWM, DAC Level, PRG Mode & rise/fall rates, a sawtooth, falling ramp, or triangle signal can be generated at various frequencies and rising/falling rates.
Here is the code to get started. I used a 4x 20 LCD display for code development/debugging. The Printbits sub displays a register in binary format. Example: Printbits PRG1CON0,0. This will print the register bits on line 0 of the LCD Display.
Could be useful to control x/y scope and bresenham's line algo to do vector graphics like asteroids was , 2 ramp generators clocked at different frequency.