outputting 5khz frequency  - Page 1
Forum section: electronics / general

author: marsal
author_rank: Newbie
timestamp: April 27, 2014, 11:08:30 pm
content: I have a pic18f45k20 chip and i am using mplax with c8 compiler and was wondering if any one can help me to construct a c code to create a 5khz frequency i habe done in using assembly but i am not familiar with c.here is my asm code:;//** I N C L U D E S ******************************/list p=18f45k20#include p18f45k20.inc;//** C O N F I G U R A T I O N B I T S ***********/CONFIG FOSC = INTIO67 ; INTIO67 Note that if it is HS it will not work!CONFIG LVP = OFFCONFIG PWRT = ONCONFIG FCMEN = OFFCONFIG IESO = OFF ;// CONFIG1HCONFIG BOREN = SBORDISCONFIG BORV = 30 ;// CONFIG2LCONFIG WDTEN = OFFCONFIG WDTPS = 32768 ;// CONFIG2HCONFIG MCLRE = OFFCONFIG LPT1OSC = OFFCONFIG PBADEN = ONCONFIG CCP2MX = PORTC ;// CONFIG3HCONFIG STVREN = ON;CONFIG XINST = OFF ;// CONFIG4LCONFIG CP0 = OFFCONFIG CP1 = OFFCONFIG CP2 = OFFCONFIG CP3 = OFF ;// CONFIG5LCONFIG CPB = OFFCONFIG CPD = OFF ;// CONFIG5HCONFIG WRT0 = OFFCONFIG WRT1 = OFFCONFIG WRT2 = OFFCONFIG WRT3 = OFF ;// CONFIG6LCONFIG WRTB = OFFCONFIG WRTC = OFFCONFIG WRTD = OFF ;// CONFIG6HCONFIG EBTR0 = OFFCONFIG EBTR1 = OFFCONFIG EBTR2 = OFFCONFIG EBTR3 = OFF ;// CONFIG7LCONFIG EBTRB = OFF;---------------------------------;Title: "Lab 5; 5kHz Blinking LED";---------------------------------;Program Detail: This program will create a square wave;at 5kHz that will correlate to the frequency of a blinking;LED on the microchip.;----------------------------------------------------------;Purpose: Blink LED at a 5kHz frequency;----------------------------------------------------------;//** INITIALIZATION **//count equ D'6' ;Variable used for counter;//Register Declaration:/reg1 equ 0x01 ;Register Declarationsreg10 equ 0x10;//M A I N C O D E://org 0x20startmovlw b'00000000' ;Set up all bits in PORT D formovwf TRISD ;outputmovwf reg1onoffmovff reg1,PORTD ;Turn on/off all bits in PORTDmovlw count ;Load counter in W and set upmovwf reg10 ;in reg10delaydecf reg10,1 ;Decrement reg10 and repeatbnz delay ;if reg10 is not equal to 0comf reg1,1 ;Complement bit pattern forbra onoff ;on/offend

author: dannyf
author_rank: Super Contributor
timestamp: April 27, 2014, 11:57:09 pm
content: The simplest would be to use the pwm generator / timers.A dumber solution would be to use NOP inbetween pin flips.

author: Dinsdale
author_rank: Regular Contributor
timestamp: May 01, 2014, 06:18:51 pm
content: This compiled for a 16F target with xc8. You need to add function prototypes and initialization. The time in the delay loop will probably be longer with same value for "count"; you'll have to figure that out. I moved your 'comf' out of the delay loop. Functionally, it does not belong there.Code:volatile unsigned char reg1 @ 0x020; // output image for port Dint main( void ) {//initialize( );blink( );}void blink( ) {TRISD = 0; // this belongs in the initialization sectionreg1 = 0;while( 1 ) {PORTD = reg1;delay( );reg1 ^= 1;}}void delay( ) {for( int count = 6; count > 0; count-- );}I was going to show your assembly on the left in the code box but proportional fonts made that a joke! Creating signals this way is a good learning experience. Let me know what an LED blinking at 5kHz looks like!