此SPI和频率计数器程序是否正确实施?

时间:2019-04-23 17:27:12

标签: c pic spi pic18

我正在PIC18F4585上设置一个频率计数器,该计数器通过8位SPI发送频率。

我正在PICF4584中设置一个频率计数器,该计数器将计算进入定时器/计数器1的方波的频率,对由定时器/计数器0计时的8ms采样,然后通过SPI发送到另一个MCU。我没有连接SPI输入引脚SDI,因为我不会发送任何数据,但是由于某些原因,我没有输出数据。我通过AVR线了解了MCU,因此PIC做事的方式与我有些不同,我想我可能会错过一些基本知识。

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

volatile int count;

void timer0_init() //for sampling period 64 prescaler@ 8MHz -> 1000 ticks per 8ms
{
    T0CON |= (1<<7); //Timer1 on
    T0CON |= (1<<2) | (1<<0); //64 prescaler

    TMR0 = 64535; //interrupt initiated on overflow, 1000 ticks until overflow
}

void timer1_init() //for frequency counting
{
    T1CON |= (1<<7); //16 bit operation
    T1CON |= (1<<1); //Timer1 clock source external RC0/T1OSO/T13CKI on rising edge
    T1CON |= (1<<0); //Enables Timer1
    T1CON |= (1<<6); //Device clock source is derived from Timer1 oscillator
}

void SPI_init()
{
    //TRIS 0 is default output, TRIS 1 is input
    TRISC &= ~(1<<3);
    TRISC &= ~(1<<5);
    TRISA |= (1<<5);
    PORTA |= (1<<5);

    SSPSTAT |= (1<<7); //Input data sampled at end of output time
    SSPSTAT |= (1<<6); //Clock: transmit occurs on active to idle

    SSPCON1 |= (1<<5); //SCK, SDO, SDI, and SS are serial port pins
    SSPCON1 |= (1<<0); //Fosc/16

}

void interrupt_init()
{
    INTCON |= (1<<7); //global interrupt enable
    INTCON |= (1<<7); //Enables high priority interrupts
    INTCON |= (1<<5); //enables Timer0 overflow
}

void __interrupt () ISR(void)
{
   count =  TMR1;
   count = (count)/.008;

   /*SPI TRANSMIT*/
   PORTA &= ~(1<<5); //Slave Select goes low
   SSPBUF = count;
   PORTA |= (1<<5);

   /*RESET TIMERS AND INTERRUPTS*/
   TMR1 = 0;
   TMR0 = 64343; 
   INTCON &= ~(1<<2);
}



int main() {
    interrupt_init();
    timer0_init();
    timer1_init();
    SPI_init();
    while(1)
    {
    }
}

0 个答案:

没有答案