Skip to content

Instantly share code, notes, and snippets.

@marioIncandeza
Last active October 11, 2015 04:39
Show Gist options
  • Save marioIncandeza/42c4a829781650a54a12 to your computer and use it in GitHub Desktop.
Save marioIncandeza/42c4a829781650a54a12 to your computer and use it in GitHub Desktop.
My first C program
// PIC16F887 Configuration Bit Settings
// 'C' source line config statements
#define _XTAL_FREQ 4000000
#include <xc.h>
#include <stdlib.h>
#include <stdio.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = HS // Oscillator Selection bits
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT not enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void C(double duration){
int reps = 1046*duration;
PORTA = 0xFF;
for (int i=0; i<reps;i++){
RC2 = 1;
__delay_us(465);
RC2 = 0;
__delay_us(464);
}
PORTA = 0;
//mean scope freq: 1046 Hz
}
void D(double duration){
int reps = 1174*duration;
PORTB = 0xFF;
for (int i=0; i<reps;i++){
RC2 = 1;
__delay_us(413);
RC2 = 0;
__delay_us(412);
}
PORTB = 0;
//mean scope freq: 1,173.6 Hz
}
void E(double duration){
int reps = 1318*duration;
PORTB = 0xFF;
for (int i=0; i<reps ;i++){
RC2 = 1;
__delay_us(366);
RC2 = 0;
__delay_us(365);
}
PORTB = 0;
//mean scope freq: 1,318.3
}
void F(double duration){
int reps = 1396*duration;
PORTA = 0xFF;
for (int i=0; i<reps ;i++){
RC2 = 1;
__delay_us(346);
RC2 = 0;
__delay_us(345);
}
PORTA = 0;
//mean scope freq: 1,393 Hz
}
void G(double duration){
int reps = 1567*duration;
PORTB = 0xFF;
for (int i=0; i<reps ;i++){
RC2 = 1;
__delay_us(306);
RC2 = 0;
__delay_us(306);
}
PORTB = 0;
//mean scope freq: 1,566.9 Hz
}
void A(double duration){
int reps = 1760*duration;
PORTA = 0xFF;
for (int i=0; i<reps ;i++){
RC2 = 1;
__delay_us(271);
RC2 = 0;
__delay_us(270);
}
PORTA = 0x00;
//mean scope freq: 1,760.1 Hz
}
void main(){
TRISC = 0;
TRISA = 0;
TRISB = 0;
ANSEL, ANSELH = 0;
PORTC = 0;
while (1){
C(.5);
__delay_ms(100);
C(.5);
__delay_ms(100);
G(.5);
__delay_ms(100);
G(.5);
__delay_ms(100);
A(.5);
__delay_ms(100);
A(.5);
__delay_ms(100);
G(1);
__delay_ms(100);
F(.5);
__delay_ms(100);
F(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
D(.5);
__delay_ms(100);
D(.5);
__delay_ms(100);
C(1);
__delay_ms(100);
G(.5);
__delay_ms(100);
G(.5);
__delay_ms(100);
F(.5);
__delay_ms(100);
F(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
D(1);
__delay_ms(100);
G(.5);
__delay_ms(100);
G(.5);
__delay_ms(100);
F(.5);
__delay_ms(100);
F(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
D(1);
__delay_ms(100);
C(.5);
__delay_ms(100);
C(.5);
__delay_ms(100);
G(.5);
__delay_ms(100);
G(.5);
__delay_ms(100);
A(.5);
__delay_ms(100);
A(.5);
__delay_ms(100);
G(1);
__delay_ms(100);
F(.5);
__delay_ms(100);
F(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
E(.5);
__delay_ms(100);
D(.5);
__delay_ms(100);
D(.5);
__delay_ms(100);
C(1);
__delay_ms(10000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment