Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sine wave from 16F628A

Status
Not open for further replies.

whiz115

Member
Hi!

I want to make a PIC output sine wave from RA2 pin. I don't care about specific frequency i want to modify the code for my needs.

If someone is kind enough to help me it would be nice... :) i could take a seat and try to do it by myself but probably i won't be able to do such stuff until 5-6 months or so and i need this code now! :D

thank you!
 
You can't output a sine wave on a single pin unless you use PWM and this would have to be on RB3 in order for it to work. Have a read of this thread, it may give you some ideas.

Mike.
 
Pommie what does this code of yours do? can i use it for my needs?

Pommie said:
Just incase anyone fancies trying this in asm, here it is.

Code:
                LIST    p=16F88
                #INCLUDE <P16F88.INC>
                        
                errorlevel -302
;------------------------------------------------------------------------------  
                        
                __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
                __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
                        
;------------------------------------------------------------------------------  
                        
                CBLOCK  0x20            ; Sample GPR variable registers allocated contiguously
Position:2              
Step:2                  
TempB                   
                ENDC    
                        
W_TEMP          EQU     0x7D            ; w register for context saving (ACCESS)
STATUS_TEMP     EQU     0x7E            ; status used for context saving (ACCESS)
PCLATH_TEMP     EQU     0x7F            ; variable used for context saving
                        
;------------------------------------------------------------------------------  
; RESET VECTOR          
;------------------------------------------------------------------------------  
                        
RESET           ORG     0x0000          ; processor reset vector9
                PAGESEL START
                GOTO    START           ; go to beginning of program
                        
;------------------------------------------------------------------------------  
; INTERRUPT SERVICE ROUTINE  
;------------------------------------------------------------------------------  
                        
ISR             ORG     0x0004          ; interrupt vector location
;         Context saving for ISR  
                MOVWF   W_TEMP          ; save off current W register contents
                MOVF    STATUS,W        ; move status register into W register
                MOVWF   STATUS_TEMP     ; save off contents of STATUS register
                MOVF    PCLATH,W        ; move pclath register into W register
                MOVWF   PCLATH_TEMP     ; save off contents of PCLATH register
                        
;------------------------------------------------------------------------------  
; USER INTERRUPT SERVICE ROUTINE GOES HERE  
;------------------------------------------------------------------------------  
                        
                movfw   Step            ;Position+=Step
                addwf   Position,F
                movfw   Step+1
                btfsc   STATUS,C
                addlw   1
                addwf   Position+1,F
                movlw   high GetSine    ;Prepare to get sine value
                movwf   PCLATH
                movfw   Position+1
                andlw   .31             ;TempB=(Position>>8)&31
                call    GetSine
                movwf   TempB
                movwf   PORTB
                        
                bcf     PIR1,TMR2IF
                        
                        
;         Restore context before returning from interrupt  
                MOVF    PCLATH_TEMP,W   ; retrieve copy of PCLATH register
                MOVWF   PCLATH          ; restore pre-isr PCLATH register contents
                MOVF    STATUS_TEMP,W   ; retrieve copy of STATUS register
                MOVWF   STATUS          ; restore pre-isr STATUS register contents
                SWAPF   W_TEMP,F
                SWAPF   W_TEMP,W        ; restore pre-isr W register contents
                RETFIE                  ; return from interrupt
                        
;------------------------------------------------------------------------------  
; MAIN PROGRAM          
;------------------------------------------------------------------------------  
                        
START                   
                bsf     STATUS,RP0
                movlw   0xf0
                movwf   TRISA
                movwf   TRISB
                movlw   b'00000111'     ;	disable comparator
                movwf   CMCON
                movlw   B'01110000'     ;select 8MHz clock
                movwf   OSCCON
                clrf    ANSEL           ;no ADCs
                bcf     STATUS,RP0
                        
                movlw   1<<TMR2ON|b'01'<<T2CKPS0
                movwf   T2CON
                bsf     STATUS,RP0
                movlw   .249
                movwf   PR2
                bsf     PIE1,TMR2IE
                bcf     STATUS,RP0
                        
                movlw   low(.819)
                movwf   Step
                movlw   high(.819)
                movwf   Step+1
                        
                movlw   (1<<GIE|1<<PEIE|0<<TMR0IE|0<<INTE|0<<RBIE|0<<TMR0IF|0<<INTF|0<<RBIF)
                movwf   INTCON          ;		enable Peripheral interupts
                        
                        
;------------------------------------------------------------------------------  
; PLACE USER PROGRAM HERE  
;------------------------------------------------------------------------------  
                        
                GOTO    $
                        
                org     0x0400
GetSine         addwf   PCL,F
                dt      .7,.8,.10,.11,.12,.13,.13,.14,.14,.14,.13,.13,.12,.11,.10,.8
                dt      .7,.6,.4,.3,.2,.1,.1,.0,.0,.0,.1,.1,.2,.3,.4,.6
                        
                END

Mike.
 
It outputs a sine wave on the lower 4 bits of Port B. An R2R DAC (google it) is required to convert to an analogue value. If you read the whole thread you should get a better idea of what it does.

Mike.
 
so... i need the above code a resistor network connected to the corresponding pins and an opamp?

and probably i need accurate resistor values 1% tolerance for that... right?
is there any easier way? you said something about using just one pin with PWM...
 
Last edited:
Yes, that will give you a noisy 200Hz output, adding a filter should make it cleaner. Note, it is only 4 bit and so is very "stepy". It would be fairly simple to change it to 8 bit and get a much smoother waveform.

Mike.
 
well... i just need a sine wave out, 200Hz it's fine with me...i don't mind if it noisy or not so accurate.

but i would like it simpler, because it's for a fast experiment and i don't have time chassing 1% resistors and setting up op-amps.. :eek: :D
 
whiz115 said:
well... i just need a sine wave out, 200Hz it's fine with me...i don't mind if it noisy or not so accurate.

but i would like it simpler, because it's for a fast experiment and i don't have time chassing 1% resistors and setting up op-amps.. :eek: :D

For only 4 bits, 5% resistors should be fine.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top