How to generate Beep sounds/Piezo using PIC16F628A

Status
Not open for further replies.

Zh3ng

New Member
May anyone expert with PIC help me on PIC16F628A? The attached code is a 8 - leds chaser code with PWM, with time delay RELAY. Port RA2 will send output/high to relay after delay approx 4 mins 30 secs. I willing to add piezo on Port RA3, so that when RELAY ON(RA2 is high) the piezo will beep 3 times. Please help me!!!! Please help me to add piezo function in the code. Thanks so much !!!!
 

Attachments

  • LED Chaser 628A EDA.zip
    93.8 KB · Views: 317
To save anyone else from needing to download the Zip file here is the ASM code it contains:

Code:
;**********************************************************************
;                                                                     *
;    8 Channel PWM LED Chaser for PIC 16F628/A and 16F84A             *
;                                                                     *
;    Filename:                  pwmc_main107.asm                      *
;    Date:                      03/04/2009                            *
;    File Version:              1.0.7                                 *
;                                                                     *
;    Author:                    Pete Griffiths                        *
;    Company:                   http://picprojects.org.uk             *
;                                                                     * 
;    Written and assmbled under MPLAB Version 8.20                    *
;                                                                     *
;    Before assembling code set correct PIC processor in MPLAB        *
;    from menu option Configure - Select Device                       *
;                                                                     *
;    Now works with 16F84A, 16F627/628 16F627A/628A and 16F88         *
;                                                                     *
;    The devices with 1K program memory (16F84A, 16F627/627A          *
;    will need some Sequences in pwmc_SeqData.inc file removing       *
;    to fit the code in their limited memory.                         *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;     1. PIC 16F628A datasheet can be downloaded from                 *
;        http://ww1.microchip.com/downloads/en/DeviceDoc/40044b.pdf   *
;                                                                     *
;     2. See also MPASM User Guide(UG) which can be downloaded from:  *
;        http://ww1.microchip.com/downloads/en/DeviceDoc/33014J.pdf   *
;        http://picprojects.org.uk/projects/ledchaser/                *
;                                                                     *
;     3. This code uses Pseudo-instructions listed in the MPASM User  *
;        guide, Part 4, Appendix A.5                                  *
;                                                                     *
;     4. For list of Radix types see MPASM UG Section 3.4, Table 3-3  *
;                                                                     *
;**********************************************************************

	ifdef __16F88

	#include <p16f88.inc>

		;Program Configuration Register 1
		__CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO

		;Program Configuration Register 2
		__CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF

	#define T0IF	TMR0IF
	#define T0IE	TMR0IE

	endif

              ifdef __16F628A

               #include <p16f628A.inc>      ; Standard include file
                                            ; defines processor specific variable definitions
                                            ; See MPASM UG Section 4.41

              ;------------------------------------------------------------------------------------
              ; '__CONFIG' directive is used to embed configuration data within .asm file.
              ; The lables following the directive are located in the respective .inc file.
              ; See respective data sheet for additional information on configuration word.
              ; and also see MPASM UG Section 4.12

               __CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _LVP_OFF 

              endif

              ifdef __16F628

               #include <p16f628.inc>
               __CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _LVP_OFF 

              endif

              ifdef __16F84A
               

	; **** 16F84A only has 1K of program memory - the default pwm_SeqData.inc file ****

	; **** will not fit into the available memory, you must remove some of the     ****

	; **** the sequence data from the file before assembling the code              ****
               #include <p16f84a.inc>
                __CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _HS_OSC
              ; Code protect off
              ; Watch Dog timer off
              ; Power-up timer delay on
              ;   change to _RC_OSC for resistor/capacitor 
              ;   change to _XT_OSC for crystal/resonator <3.5Mhz
              ;   change to _HS_OSC for high speed crystal/resonator >3.5Mhz

              endif

              ;------------------------------------------------------------------------------------
              ; Suppress specific warning messages
              ; see MPASM UG Section 4.29 for errorlevel directive       
              ; see MPASM UG Section 8.4 for assembler message descriptions
              ;




              errorlevel -302     ; suppress banksel warning messages during assembly


              ;------------------------------------------------------------------------------------
              ; define variables in General Purpose Register (GPR) memory
              ; See MPASM UG Section 4.8
              ; note: 16F84A GPR start at 0x0C but most other mid-range PICs start at 0x20
              ; We use 0x20 here for compatibilty with newer PICs

              cblock 0x20
                firstGPR:0                ; mark start of GPR memory used
                state                     ; used by function state selector
                copyPORTB                 ; working variable, holds copy of PORTB
                vc0,vc1,vc2,vc3,vc4       ; vertical counter bits 
                hiReload                  ; vertical counter reload hi 
                loReload                  ; vertical counter reload lo
                pwm                       ; pwm counter
                holdTime                  ; sequence line hold time
                repeatCount               ; repeat seqeunce count
                RandMask                  ; mask to AND random number with
                seqIdxLo                  ; Lo byte address index pointer to start of selected sequence  
                seqIdxHi                  ; Hi byte address index pointer to start of selected sequence
                seqTotal                  ; Total number of sequences in data
                seqMatch                  ; working variable
                seqCount                  ; working variable
                forward                   ; working regsiter for mirroring data
                reverse                   ; working regsiter for mirroring data
                reloadTemp                ; temporary holding variable for loReload
                tick                      ; timer0 ticks 
                swTimer                   ; switch hold down timer
                flags                     ; function flags register
	  			mode					  ; mode flags register
                indexLo                   ; Lo byte sequence line data address pointer
                indexHi                   ; Hi byte sequence line data address pointer
                save_W                    ; save W during interupt
                save_Status               ; save STATUS during interupt
                save_PCLATH               ; save PCLATH
	  			saveModeTimerL			  ; holdoff timer for saving opmode to EEPROM high
	  			saveModeTimerH			  ; holdoff timer for saving opmode to EEPROM low
	  			eesave_W				  ; temp register used in EEPROM routings
                LFSRH                     ; Random number shift regsiter high byte
                LFSRL:0                   ; Random number shift register low byte
                lastGPR                   ; mark last GPR register used 
                                          ; (required by GPR initialisation code)



				D1

				D2

				

				INTC

				INTC1

				count

              endc







;--------------------------------------------------------------------------------------------------------------------
; EEPROM data

; 	; program parameters
	org 0x2100
	de .0
	de .1

	de .0



	; embed firmware release info
	de " PWM LED Chaser "
	de " V1.0.7 "

	de " 03/04/2009 "

	de " molek electronics "
;--------------------------------------------------------------------------------------------------------------------

; Define bank select pseudo instructions to make code more readable
#define   bank0     bcf       STATUS,RP0          ; Sel Bank 0
#define   bank1     bsf       STATUS,RP0          ; Sel Bank 1

;--------------------------------------------------------------------------------------------------------------------
;
; See MPASM UG Secdtion 4.27 for definition of equ directive

switch        equ 4         ; Port bit switch connected to

cSAVETIME EQU	  d'4'	; holdoff timer constant for saving EEPROM data
cTIMER    EQU   d'157'
cTICKS    EQU   d'50'

temp1	  EQU   d'1'

z         EQU   d'1'

mhz		  EQU   D'4'  ; processor frequency in MHz
; 4Mhz oscillator clock, gives TMR0 clock = 1Mhz.
; Write to timer0 inhibits clock for 2 clock cycles
; TMR0 prescaler of 1:2 gives
; 1Mhz / ((256-157+2) x 2) = 5Khz = 0.2mS
; 50 x 0.2mS = 10mS
; So the sequence hold time will be 10mS x holdTime, giving a range of
; 1 = 10mS thru 254 = 2.54S
; remember that a holdtime value of 255 indicates the end of the sequence so DON'T USE IT

; define flag bits
fHoldTimeout  EQU           0             ; flag set by ISR, cleared by Table Lookup function
fSwitch       EQU           1             ; flag 
fsetupRun     EQU           2             ; 0=run mode / 1=setup mode
fTick         EQU           3             ; timer tick flag
fSaveMode	  EQU			4			  ; set indicates opmode needs writing to EEPROM, clear indicates no data to write

;							5
fMirrorData   EQU           6             ; flag set, Table read reverses bits in data word [** This flag must be Bit 6 **]
fMirrorNext   EQU           7             ; flag tells code current sequence should be mirrored next time

#define   canMirror 1<<fMirrorData        ; Don't change the position of this bit, sequence data expects it here

; define mode bits
modeSeq		EQU	0
modeRan		EQU	1
modeMan		EQU	2
modeSleep	EQU	3



dcnt0		RES 1 ; delay counter 0

dcnt1		RES 1 ; delay counter 1

beepcnt		RES 2 ; beep cycle counter









;--------------------------------------------------------------------------------------------------------------------
; Program code starts here
;
; See MPASM UG Section 1.7.1, and Example 1-1
;
; Labels      Mnemonics     Operands                     Comments
;             Directives
;             Macros
;


RESET_VECTOR  org           0x000                       ; see MPASM UG Section 4.50

			  goto          START                       ; START label is in pwmc_start.inc

INTERRUPT_V   org           0x004                       ; Interrupt handler code must follow this origin statement

              

			  #include pwmc_inth.inc                    ; include Interrupt Handler code block
              #include pwmc_statef.inc                  ; include main state function code block (must reside in page 0)
     		  #include pwmc_start.inc                   ; include reset and initialisation code         

              #include pwmc_functions.inc               ; include all program functions
              #include pwmc_SeqMacro.inc                ; include MACRO definitions for sequence data 
                                                        ; used for creating sequence data tables
			  
; ------------------------------------------------------------------------------------------------------





;=======================================================

ISR

   DECFSZ   INTC 	 ;CHECK FOR 46 TIMES INTERRUPT OCCURRED

   GOTO     $+4 	 ;DON'T TURN RELAY ON

   MOVLW	.1	

   MOVWF	count

   CLRF 	PIR1	

   BCF      PIR1, TMR1IF 

RETURN

;======================================================= 



ISR1

   DECFSZ   INTC1 	 ;CHECK FOR 46 TIMES INTERRUPT OCCURRED

   GOTO     $+2 	 ;DON'T TURN RELAY ON	

   BSF      PORTA, 2 ;TURN RELAY ON and LED

   BCF      PIR1, TMR1IF   

RETURN



; DO NOT CHANGE OR REMOVE this label
SeqBaseAddr   ; label marks base address for start of sequence data.  

              ; Include your sequence data file(s) here
              
	#include pwmc_SeqData.inc                 			; file containg sequence data 
              ;#include pwmc_xmastree.inc               ; alternate data for use with xmas tree pcb
              ;#include pwmc_round.inc                  ; alternate data for use with the round pcb

              ; Do not remove this instruction
_markLast     retlw         .255          ; Ensure last sequence is marked
; -----------------------------------------------------------------------------------------------------


              end           ; end of instructions, always needed
                            ; see MPASM UG Section 4.22
 
Here is a program that already detects a button press and has a number of beeps as sub-routines. It is much easier to delete sub-routines than add them.
 

Attachments

  • SimonF84.asm
    24.6 KB · Views: 346
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…