Advertisement
chasxmd

bit seperating, PIC with DAC

Jan 8th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.87 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: Charles M Douvier
  4.  * Contact at: http://iradan.com
  5.  *
  6.  * Created on February 8, 2014, 11:39 AM
  7.  *
  8.  * Target Device:
  9.  * 18F14K22 on Tautic 20 pin dev board
  10.  *
  11.  * Project: MIDI Slave
  12.  *
  13.  *
  14.  * Version:
  15.  * 0.1  Configuration, 31.25Kbaud TX&RX
  16.  * 0.2  SPI output works
  17.  * 0.3
  18.  *
  19.  */
  20. #ifndef _XTAL_FREQ
  21. #define _XTAL_FREQ 16000000 //4Mhz FRC internal osc
  22. #define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
  23. #define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
  24. #endif
  25.  
  26. #include <xc.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31.  
  32. //config bits
  33. #pragma config FOSC=IRC, WDTEN=OFF, PWRTEN=OFF, MCLRE=ON, CP0=OFF, CP1=OFF, BOREN=ON
  34. #pragma config STVREN=ON, LVP=OFF, HFOFST=OFF, IESO=OFF, FCMEN=OFF
  35.  
  36. #define _XTAL_FREQ 16000000 //defined for delay
  37.  
  38. /*
  39.  * Variables
  40.  */
  41.  
  42.     //long int    decm;                 //long temp
  43.     int     spi_msb, spi_lsb;           //data
  44.     int     i, x;                          //counter
  45.     int     itxdata, ilevel;            //int UART data
  46.     char    buf[10];            //buff for iota
  47.     volatile unsigned int uart_data;    // use 'volatile' qualifer as this is changed in ISR
  48.  
  49. /*
  50.  *  Functions
  51.  */
  52.  
  53.     void interrupt ISR() {
  54.  
  55.     if (PIR1bits.RCIF)          // see if interrupt caused by incoming data
  56.     {
  57.         uart_data = RCREG;     // read the incoming data
  58.         PIR1bits.RCIF = 0;      // clear interrupt flag
  59.                                 //
  60.     }
  61.  
  62. }
  63.  
  64. void uart_xmit(unsigned int mydata_byte) {
  65.  
  66.     while(!TXSTAbits.TRMT);    // make sure buffer full bit is high before transmitting
  67.     TXREG = mydata_byte;       // transmit data
  68. }
  69.  
  70. void serial_init(void)
  71. {
  72.  
  73.     // calculate values of SPBRGL and SPBRGH based on the desired baud rate
  74.     //
  75.     // For 8 bit Async mode with BRGH=0: Desired Baud rate = Fosc/64([SPBRGH:SPBRGL]+1)
  76.     // For 8 bit Async mode with BRGH=1: Desired Baud rate = Fosc/16([SPBRGH:SPBRGL]+1)
  77.  
  78.  
  79.  
  80.     TXSTAbits.BRGH=1;       // select low speed Baud Rate (see baud rate calcs below)
  81.     TXSTAbits.TX9=0;        // select 8 data bits
  82.     TXSTAbits.TXEN = 1;     // enable transmit
  83.  
  84.  
  85.     RCSTAbits.SPEN=1;       // serial port is enabled
  86.     RCSTAbits.RX9=0;        // select 8 data bits
  87.     RCSTAbits.CREN=1;       // receive enabled
  88.  
  89.     //BRGH=1        31.25KHz
  90.     //SPBRG=7
  91.  
  92.     SPBRG=7;               //
  93.  
  94.     PIR1bits.RCIF=0;        // make sure receive interrupt flag is clear
  95.     PIE1bits.RCIE=1;        // enable UART Receive interrupt
  96.     INTCONbits.PEIE = 1;    // Enable peripheral interrupt
  97.     INTCONbits.GIE = 1;     // enable global interrupt
  98.  
  99.          __delay_ms(10);        // give time for voltage levels on board to settle
  100.  
  101.     //  uart_xmit('R');         // transmit a character example
  102.  
  103. }
  104.  
  105. void spi_write(unsigned int data_byte) {
  106.  
  107.             LATCbits.LATC2 = 0;     //set CS low (start chip select)
  108.             __delay_us(0);
  109.            
  110.             SSPBUF = data_byte;
  111.             __delay_us(9);     //!SSPSTATbits.BF does not work
  112.  
  113. }
  114.  
  115. void spi_stop(void) {
  116.     LATCbits.LATC2 = 1;     // set CS high (end transmission)
  117. }
  118.  
  119.  
  120. void spi_init(void) {
  121.     SSPCON1bits.WCOL = 0;        //  Just in case..
  122.     SSPCON1bits.SSPM = 0x00;        //  FOSC/4
  123.     SSPCON1bits.CKP = 0;         //  SPI Mode 0
  124.     SSPSTATbits.CKE = 1;
  125.     SSPSTATbits.SMP = 1;
  126.     SSPCON1bits.SSPEN = 1;       //  SPI GO!
  127. }
  128.  
  129.  
  130.  
  131. void init_io(void) {
  132.     TRISAbits.TRISA0 = 0; // output
  133.     TRISAbits.TRISA1 = 0; // output
  134.     TRISAbits.TRISA2 = 0; // output
  135.     TRISAbits.TRISA4 = 0; // output
  136.     TRISAbits.TRISA5 = 0; // output
  137.  
  138.     ANSEL = 0x00;         // no A/D
  139.     ANSELH = 0x00;
  140.    
  141.     TRISBbits.TRISB4 = 1; // SPI SDI
  142.     TRISBbits.TRISB5 = 1; // UART RX
  143.     TRISBbits.TRISB6 = 0; // SPI SCK
  144.     TRISBbits.TRISB7 = 0; // SPI SDO
  145.  
  146.     TRISCbits.TRISC0 = 0; // LED
  147.     TRISCbits.TRISC1 = 0; // output
  148.     TRISCbits.TRISC2 = 0; // DAC /CS CHIP SELECT
  149.     TRISCbits.TRISC3 = 0; // output
  150.     TRISCbits.TRISC4 = 0; // output
  151.     TRISCbits.TRISC5 = 0; // output
  152.     TRISCbits.TRISC6 = 0; // SPI SS
  153.     TRISCbits.TRISC7 = 0; // SPI SDO
  154.  
  155. }
  156.  
  157. void set_cmd11(void)
  158. {
  159.     //set an AD output
  160.                 while(uart_data==0x11)
  161.             {
  162.                 i++; //wait for next char
  163.             }
  164.             ilevel = uart_data;
  165.             uart_xmit(ilevel);
  166. }
  167.  
  168. void check0xb1(void)
  169. {
  170.     if (uart_data == 0xB1)
  171.     {
  172.             while(uart_data==0xB1)
  173.             {
  174.                 i++; //wait for next char
  175.             }
  176.             if ( uart_data == 0x11)
  177.                set_cmd11();
  178.     }
  179. }
  180.  
  181. int main(void) {
  182.  
  183.     init_io();
  184.  
  185.     // set up oscillator control register, using internal OSC at 16MHz.
  186.     OSCCONbits.IRCF = 0x07; //set OSCCON IRCF bits to select OSC frequency 16MHz
  187.     OSCCONbits.SCS = 0x02; //set the SCS bits to select internal oscillator block
  188.  
  189.     SSPCON1bits.SSPEN = 0;       //  SPI GO!
  190.  
  191.  
  192.     serial_init();
  193.     spi_init();
  194.  
  195.     LATAbits.LATA0=0;
  196.  
  197.     LATCbits.LATC2 = 1;     //turn off CS on DAC
  198.  
  199.     while (1) {
  200.  
  201.             x++;
  202.             i = x;
  203.    //         __delay_ms(1);
  204.  
  205.             spi_msb = ((i >> 8) & 0xff);    //pick i apart
  206.             spi_lsb = ((i >> 0) & 0xff);
  207.  
  208.             spi_msb = spi_msb + 0x50;       //add configuration
  209.  
  210.             //spi_msb = 0x50;
  211.            
  212.             spi_write(spi_msb);        //my spi_write always sets /CS but if I drop it the
  213.             spi_write(spi_lsb);        //comm inbetween a comm the comm doesn't work
  214.                                     //so you manually have to stop /CS spi_stop()
  215.  
  216.             spi_stop();     //CS
  217.  
  218.             if (x > 0x01FE) {
  219.                 x = 0x00;
  220.             }
  221.            
  222.         //while (uart_data)
  223.         //{
  224.             //check0xb1();
  225.             //uart_data=0;
  226.         //}
  227.  
  228.     }
  229.     return (EXIT_SUCCESS);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement