XC8 Bluetooth (12F1822 + HC06)

Başlatan baran123, 08 Temmuz 2015, 22:34:39

baran123

XC8 den bayağı zevk almaya başladım. :D
Test ettim çalışıyor.

main.h
/* 
 * File:   main.c
 * Author: Baran EKREM
 * Created on 08 Temmuz 2015 Çarşamba, 21:07
 */
#ifndef MAIN_H
#define	MAIN_H

#include <xc.h>
#include <pic12f1822.h>
#include <stdio.h>
#include <stdlib.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF      // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

#define _XTAL_FREQ 32000000L


#define PORT_TX     PORTAbits.RA0
#define PORT_RX     PORTAbits.RA1
#define PORT_LED    PORTAbits.RA2

#define TRIS_TX     TRISAbits.TRISA0
#define TRIS_RX     TRISAbits.TRISA1
#define TRIS_LED    TRISAbits.TRISA2

#endif


main.c
/* 
 * File:   main.c
 * Author: Baran EKREM
 * Created on 08 Temmuz 2015 Çarşamba, 21:07
 */
#include "main.h"

void Init_MCU(void);
void Init_UART(void);
void UART_SendData(char data);
void UART_SendString(char* s);
void Delay_ms(unsigned int second);

char RxData;
int flag = 0;

void interrupt ISR(void) {
    if(PIE1bits.RCIE) {
        if(PIR1bits.RCIF) {
            PIR1bits.RCIF = 0;
             RxData = RCREG;
             flag = 1;
        }
    }
}

void main(void) {
    Init_MCU();
    Init_UART();
    
    while(1) {
        if(flag == 1) {
            if(RxData == '1') {
                PORT_LED = 1;
                UART_SendString("LED On\r\n");
            }
            if(RxData == '0') {
                PORT_LED = 0;
                UART_SendString("LED Off\r\n");
            }
            flag = 0;
        }
    }
}

void Init_MCU(void) {
    OSCCONbits.IRCF = 0b1110;
    OSCCONbits.SPLLEN = 0b1;
    
    SSP1CON1bits.SSPEN = 0;
    
    Delay_ms(1000);
    
    while (!OSCSTATbits.PLLR);
    while (!OSCSTATbits.HFIOFR);

    ANSELA = 0x00;  // Set all pin as Digital output
    
    TRIS_LED = 0;
    PORT_LED = 0;
}

void Init_UART(void) {
    BAUDCONbits.BRG16 = 1;
    SPBRG = 207;
    
    TXSTAbits.TXEN = 1;
    RCSTAbits.CREN = 1;
    RCSTAbits.SPEN = 1;
    
    PIR1bits.TXIF = 0;
    PIR1bits.RCIF = 0;
    PIE1bits.RCIE = 1;
    
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
    
    TRIS_RX = 1; //TX pin set as output
    TRIS_TX = 0; //RX pin set as input
}

void UART_SendData(char data) {
    while(!TRMT);
    TXREG = data;
}

void UART_SendString(char* data) {
    while(*data) {
        UART_SendData(*data);
        data++;
    }
}

void Delay_ms(unsigned int second) {
    unsigned int i;
    for(i=0; i < second; i++)
        __delay_ms(1);
}


HC06 TX   - PIC A1
HC06 RX   - PIC A0
LED           - PIC A2
VDD <- 10k PIC A3 (MCLR)
PIC 8 GND
PIC 1 VDD
Dahili OSC kullanılıyor (32 MHz)

mehmet

Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr
https://creativecommons.org/licenses/by/4.0/deed.tr "CC BY"

msk

UART fonksiyonları ile ilgili biraz bilgi verir misiniz? (Kullanımları gibi ... )

baran123

Yakında XC8 icin bir kac kutuphane paylasacagim.Bitirdig zaman hepsini aciklarim. :)
Fonksiyona pointerli plarak calisiyor mesela
("metin\r\n");
Böyle kullanilabilir.