Author Topic: outputting 5khz frequency  (Read 2159 times)

0 Members and 1 Guest are viewing this topic.

Offline marsalTopic starter

  • Newbie
  • Posts: 8
outputting 5khz frequency
« on: April 27, 2014, 11:08:30 pm »
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 = OFF
 CONFIG   PWRT = ON

 CONFIG FCMEN = OFF
 CONFIG IESO = OFF ;// CONFIG1H

 CONFIG BOREN = SBORDIS
 CONFIG BORV = 30 ;// CONFIG2L
 CONFIG WDTEN = OFF
 CONFIG WDTPS = 32768 ;// CONFIG2H
 CONFIG MCLRE = OFF
 CONFIG LPT1OSC = OFF
 CONFIG PBADEN = ON
 CONFIG CCP2MX = PORTC ;// CONFIG3H
 CONFIG STVREN = ON
;
 CONFIG XINST = OFF ;// CONFIG4L
 CONFIG CP0 = OFF
 CONFIG CP1 = OFF
 CONFIG CP2 = OFF
 CONFIG CP3 = OFF ;// CONFIG5L
 CONFIG CPB = OFF
 CONFIG CPD = OFF ;// CONFIG5H
 CONFIG WRT0 = OFF
 CONFIG WRT1 = OFF
 CONFIG WRT2 = OFF
 CONFIG WRT3 = OFF ;// CONFIG6L
 CONFIG WRTB = OFF
 CONFIG  WRTC = OFF
 CONFIG WRTD = OFF ;// CONFIG6H
 CONFIG EBTR0 = OFF
 CONFIG EBTR1 = OFF
 CONFIG EBTR2 = OFF
 CONFIG EBTR3 = OFF ;// CONFIG7L
 CONFIG 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 Declarations
reg10   equ 0x10


;//M A I N C O D E://

        org 0x20

start
    movlw   b'00000000'     ;Set up all bits in PORT D for
    movwf   TRISD           ;output
    movwf   reg1

onoff
    movff   reg1,PORTD      ;Turn on/off all bits in PORTD
    movlw   count           ;Load counter in W and set up
    movwf   reg10           ;in reg10

delay
    decf    reg10,1         ;Decrement reg10 and repeat
    bnz     delay           ;if reg10 is not equal to 0
    comf    reg1,1          ;Complement bit pattern for
    bra     onoff           ;on/off
    end
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: outputting 5khz frequency
« Reply #1 on: April 27, 2014, 11:57:09 pm »
The simplest would be to use the pwm generator / timers.

A dumber solution would be to use NOP inbetween pin flips.

================================
https://dannyelectronics.wordpress.com/
 

Offline Dinsdale

  • Regular Contributor
  • *
  • Posts: 77
  • Country: us
    • pretzelogic
Re: outputting 5khz frequency
« Reply #2 on: May 01, 2014, 06:18:51 pm »
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: [Select]
volatile unsigned char  reg1     @ 0x020;   // output image for port D

int main( void ) {
    //initialize( );
    blink( );
}

void blink( ) {

    TRISD = 0;                // this belongs in the initialization section
    reg1  = 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!
This can't be happening.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf