Hello! I'm hoping someone could help me with this project I've been working on. I initially made and programmed a device using a PIC12F629 to simulate muzzle flash on a prop firearm. Now, I need to make a similar unit with sound playback. So far I've based my program on this project, though I am using a PIC16F1938 for the extra I/O pins that I will be needing later for a numeric display. My goal is to have it flash an LED, and play a very small (less than 70KB) WAV file every time the trigger is pressed. Also, I'm using Source Boost C++.
This is the original main.c designed for a PIC16F1825 in that project;
So far, this is what I have modified it to, and included my muzzle flash coding. I know there's definitely something I'm not catching here, so please let me know any errors you might see. Also, I need to figure out how exactly to activate sound playback after the LED flash.
If you need to see my common.h or sd.h files, I can post them too. Thanks in advance!
This is the original main.c designed for a PIC16F1825 in that project;
Rich (BB code):
#include "common.h"
#include "SD.h"
#define FLAG_TIMEOUT 0x80
#define FLAG_PARAM_ERR 0x40
#define FLAG_ADDR_ERR 0x20
#define FLAG_ERZ_SEQ_ERR 0x10
#define FLAG_CMD_CRC_ERR 0x08
#define FLAG_ILLEGAL_CMD 0x04
#define FLAG_ERZ_RST 0x02
#define FLAG_IN_IDLE_MODE 0x01
#pragma CLOCK_FREQ 32000000
#pragma DATA _CONFIG1, _FOSC_INTOSC & _WDTE_SWDTEN & _PWRTE_ON & _MCLRE_OFF & _CP_ON & _CPD_ON & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
#pragma DATA _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _LVP_OFF
void UARTInit(){
//
// UART
//
baudcon.4 = 0; // SCKP synchronous bit polarity
baudcon.3 = 0; // BRG16 enable 16 bit brg
baudcon.1 = 0; // WUE wake up enable off
baudcon.0 = 0; // ABDEN auto baud detect
txsta.6 = 0; // TX9 8 bit transmission
txsta.5 = 1; // TXEN transmit enable
txsta.4 = 0; // SYNC async mode
txsta.3 = 0; // SEDNB break character
txsta.2 = 0; // BRGH high baudrate
txsta.0 = 0; // TX9D bit 9
rcsta.7 = 1; // SPEN serial port enable
rcsta.6 = 0; // RX9 8 bit operation
rcsta.5 = 1; // SREN enable receiver
rcsta.4 = 1; // CREN continuous receive enable
spbrgh = 0; // brg high byte
spbrg = 51; // brg low byte ()
apfcon0.2=1; // tx onto RA.0
}
void UARTSend(unsigned char c){
txreg = c;
while(!txsta.1);
}
void UARTPrint(char *s){
while(*s) {
UARTSend(*s);
s++;
}
}
void UARTPrint(char c){UARTSend(c);}
void UARTPrintNumber(unsigned long n){
unsigned long k=1000000000;
while(k>0) {
UARTSend('0'+n/k);
n%=k;
k/=10;
}
}
void UARTPrintLn(char *s){
UARTPrint(s);
UARTPrint("\r\n");
}
void UARTByte(unsigned char b){
const char *hex="0123456789abcdef";
UARTPrint("0x");
UARTSend(hex[b>>4]);
UARTSend(hex[b & 0xf]);
}
void UARTLog(char *s, unsigned short iv){
UARTPrint(s);
UARTPrint(" ");
UARTPrintNumber(iv);
UARTPrintLn(" ");
}
void fatal(UInt8 val){ //fatal error: flash led then go to sleep
UInt8 i, j, k;
for(j = 0; j < 5; j++){
for(k = 0; k < val; k++)
{
delay_ms(100);
P_LED = 1;
delay_ms(100);
P_LED = 0;
}
UARTLog("Error",val);
delay_ms(250);
delay_ms(250);
}
while(1){
asm sleep
}
}
So far, this is what I have modified it to, and included my muzzle flash coding. I know there's definitely something I'm not catching here, so please let me know any errors you might see. Also, I need to figure out how exactly to activate sound playback after the LED flash.
Rich (BB code):
#include "common.h"
#include "SD.h"
#include "PIC16F1938.h"
#include "BoostC.h"
#define FLAG_TIMEOUT 0x80
#define FLAG_PARAM_ERR 0x40
#define FLAG_ADDR_ERR 0x20
#define FLAG_ERZ_SEQ_ERR 0x10
#define FLAG_CMD_CRC_ERR 0x08
#define FLAG_ILLEGAL_CMD 0x04
#define FLAG_ERZ_RST 0x02
#define FLAG_IN_IDLE_MODE 0x01
#pragma CLOCK_FREQ 32000000
#pragma DATA _CONFIG1,
_FOSC_INTOSC & // INTOSC oscillator: I/O Functions
_WDTE_OFF & // WDT Disabled
_PWRTE_ON & // PWRT Enabled
_MCLRE_OFF & // MCLR/VPP pin function is Input
_CP_OFF & // Program memory code protection is disabled
_CPD_OFF & // Data memory code protection is disabled
_BOREN_NSLEEP & // Brown-out Reset Disabled During Sleep
_CLKOUTEN_OFF & // CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
_IESO_OFF & // Internal/External Switchover mode is disabled
_FCMEN_OFF // Fail-Safe Clock Monitor is disabled
#pragma DATA _CONFIG2,
_WRT_OFF & // Write protection off
_VCAPEN_RA0 & // VCAP on RA0
_PLLEN_OFF & // 4x PLL disabled
_STVREN_ON & // Stack Overflow or Underflow will cause a Reset
_BORV_25 & // Brown-out Reset Voltage (VBOR) set to 2.5 V
_LVP_OFF // High-voltage on MCLR/VPP must be used for programming
void MuzzleFlash(void)
{
ansela = 0b00000000; // ANSELA all Digital
trisa = 0b00000111; // Set Inputs
porta = 0b00000000; // All Outputs are 0
for(;;) // Forever
{
if(TRIG ==0) // Debounce Check
{
delay_ms(50);
if(TRIG == 0) // While TRIG switch held low
{
MUZZ_LED = 0; // MUZZ LED Off
delay_ms(10);
MUZZ_LED = 1; // MUZZ LED On
delay_ms(35);
MUZZ_LED = 0; // MUZZ LED Off
delay_ms(122);
}
}
}
}
void UARTInit()
{
//
// UART
//
baudcon.4 = 0; // SCKP synchronous bit polarity
baudcon.3 = 0; // BRG16 enable 16 bit brg
baudcon.1 = 0; // WUE wake up enable off
baudcon.0 = 0; // ABDEN auto baud detect
txsta.6 = 0; // TX9 bit transmission
txsta.5 = 1; // TXEN transmit enable
txsta.4 = 0; // SYNC async mode
txsta.3 = 0; // SEDNB break character
txsta.2 = 0; // BRGH high baudrate
txsta.0 = 0; // TX9D bit 9
rcsta.7 = 1; // SPEN serial port enable
rcsta.6 = 0; // RX9 8 bit operation
rcsta.5 = 1; // SREN enable receiver
rcsta.4 = 1; // CREN continuous receive enable
spbrgh = 0; // brg high byte
spbrg = 51; // brg low byte ()
apfcon.1 = 1; // SSSEL SS on RA0
}
void UARTSend(unsigned char c)
{
txreg = c;
while(!txsta.1);
}
void UARTPrint(char *s)
{
while(*s)
{
UARTSend(*s);
s++;
}
}
void UARTPrint(char c)
{
UARTSend(c);
}
void UARTPrintNumber(unsigned long n)
{
unsigned long k=1000000000;
while(k>0)
{
UARTSend('0'+n/k);
n%=k;
k/=10;
}
}
void UARTPrintLn(char *s)
{
UARTPrint(s);
UARTPrint("\r\n");
}
void UARTByte(unsigned char b)
{
const char *hex="0123456789abcdef";
UARTPrint("0x");
UARTSend(hex[b>>4]);
UARTSend(hex[b & 0xf]);
}
void UARTLog(char *s, unsigned short iv)
{
UARTPrint(s);
UARTPrint(" ");
UARTPrintNumber(iv);
UARTPrintLn(" ");
}
void fatal(UInt8 val) //fatal error: flash led then go to sleep
{
UInt8 i, j, k;
for(j = 0; j < 5; j++)
{
for(k = 0; k < val; k++)
{
delay_ms(100);
ERROR_LED = 1;
delay_ms(100);
ERROR_LED = 0;
}
UARTLog("Error",val);
delay_ms(250);
delay_ms(250);
}
while(1)
{
asm sleep
}
}