MCU是PIC16F648A,编译器是“HI-TECH C”,用mplab ide编译是出错,提示如下
Build C:UsersAdministratorDesktopdebug5.7 main for device 16F648A
Using driver C:Program FilesHI-TECH SoftwarePICC9.70inpicc.exe
Make: The target "C:UsersAdministratorDesktopdebug5.7 main.p1" is up to date.
Make: The target "C:UsersAdministratorDesktopdebug imer.p1" is up to date.
Make: The target "C:UsersAdministratorDesktopdebug5.1 sio.p1" is up to date.
Executing: "C:Program FilesHI-TECH SoftwarePICC9.70inpicc.exe" "-o5.7 main.cof" "-m5.7 main.map" --summary=default --output=default "5.7 main.p1" timer.p1 "5.1 sio.p1" --chip=16F648A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
HI-TECH C Compiler for PIC10/12/16 MCUs (PRO Mode) V9.70
Copyright (C) 2009 Microchip Technology Inc.
Serial number: HCPICP-45553 (PRO)
Error [1098] C:UsersAdministratorDesktopdebug5.1 sio.c; 71. conflicting declarations for variable "_sio_tx" (C:UsersAdministratorDesktopdebug5.7 main.c:62)
********** Build failed! **********
“conflicting declarations for variable ”的意思是“相矛盾的变量声明”,但我不知道错在哪里?(我是菜鸟级,大虾们不要笑我)
代码如下:
【main代码】
#include <htc.h>
#include "timer.h"
#include "sio.h"
#define PR2_DEF 24
// PWM_cycle = (PR2 + 1) * 4 * Tosc *prescale
// PR2 = ( (1/40000) / ( (4*1) /4000000) )-1
// =(0.000025 , / 0.000001) - 1 = 24
#define CCP_DEF 50
//Duty_cycle = (CCPR1L:CCP1C0N<5:4>)*Tosc * prescale
// (CCPR1L:CCP1C0N<5:4>) = (1/ (40000*2))/(1/4000000)
//= 0.0000125 / 0.00000025 = 50
#define WAVE_BIT (1<<3)
#define WAVE_ON TMR2ON=1 //Enable Timer2 On bit
#define WAVE_OFF TMR2ON=0 //Enable Timer2 On bit
//一一-一中断程序
void interrupt isr(void)
{
serial_intr();
}
//—-----------------
void wave_init (void)
{
PR2 = PR2_DEF: // Timer2 Period Register
CCP1CON = (CCP_DEF & 0b11) + 0xc;
//CCP OPERATION REGISTER
//bit5?4:duty cycle bitl?0
//bit3?0:1100=PWM mode
CCPR1L = (CCP_DEF>>2);
//Capture/Compare/PWM Register 1 (LSB)
//bit7-0:duty cycle bit9?2
T2CON = (0b0000<<3)|(0b00<<0);
//Timer2 Output Post scale Select bits ;
//bit6-3 : 0000 =7 postscale 1:1
//Tiiner2 Clock Prescale Select bits
//bitl?0: 00=prescale 1:1
TMR2IF=0; //TMR2 to PR2 Match Interrupt Flag bit
WAVE_ON;
TRISB &=~WAVE_BIT; //output PORTB bit3
}
//-----------------------------
void main (void)
{
unsigned int usec;
int mmeter;
sio_init();
timer_init ();
wave_init ();
CMCON = 0b100; //Two independent comparators
while(1) {
WAVE_ON;
timer_set(20000);
while(TMR1IF==0) {
if(C1OUT==0) break; //Clvin+ < Clvin-
usec=timer_get();
usec=usec-(0xFFFF-20000);
WAVE_OFF;
mmeter = (int) (((long)usec * 346L) /1000L);
mmeter-=110; //补正
mmeter /= 2;
if (mmeter > 0xff) mmeter=0xff;
sio_tx(mmeter & 0xff);
while (timer_overcheck()== 0) {
}
while(1) {
if(C1OUT == 1)break; //Clvin+ > Clvin-
}
}
}
}
sio.c代码:
#include<htc.h>
#include "sio.h"
#define CPUCLK 4000000L //时钟
#define BAUDRATE 9600L //波特率
#define RXBUFSIZE 10 //ring buffer size
unsigned char rx_buf[RXBUFSIZE];
unsigned char rx_cnta=0;
unsigned char rx_cntb=0;
#define TXBUFSIZE 4 //ring buffer size
unsigned char tx_buf[TXBUFSIZE];
unsigned char tx_cnta=0;
unsigned char tx_cntb=0;
//sio接收/发送中断控制
void serial_intr(void)
{
if(RCIF) {
//USART Receive Interrupt Flag bit (0=buffer empty/l=buffer full)
rx_buf[rx_cnta++] = RCREG; //RCIF=0;
if(rx_cnta >= RXBUFSIZE)
rx_cnta = 0; //ringbuff loop
}
if(TXIF){
//USART Transmit Interrupt Flag bit (0=buffer full/l=buffer empty)
if(tx_cnta == tx_cntb) //complete
{
TXIE = 0;
return;
}
TXREG = tx_buf[tx_cntb++];
if(tx_cntb >= TXBUFSIZE) tx_cntb=0; //ringbuff
}
}
//----------------------------------接收检测
//返回值:0=无接收数据/丨=有接收数据
char sio_rxcheck(void)
{
if(rx_cnta != rx_cntb)
return(1);
return(0);
}
//----------------------------------接收lByte
//返回值:接收的数据*若未收到数据,返回值为0
char sio_rx(void)
{
char ch;
if(rx_cnta == rx_cntb)
return(0); //no data
ch = rx_buf[rx_cntb++];
if(rx_cntb >= RXBUFSIZE) rx_cntb=0; //rotation ringbuff
return(ch);
}
//----------------------------sio 初始
void sio_init(void)
{
SPBRG = (CPUCLK - (BAUDRATE*16))/(BAUDRATE*16);
TXEN = 1; //Transmit Enable bit(0=disable / l=enable)
BRGH = 1; //High Baud Rate Select bit (0-Low speed / l=High speed)
SPEN = 1; //Serial Port Enable bit(0=disable / l=enable)
RX9 = 0; //9-bit Receive Enable bit(0=8bit / l=9bit)
SREN = 0; //Single Receive Enable bit(0=disable / l=enable)
CREN = 1; //Continuous Receive Enable bit (0=disable / l=enable)
RCIF = 0; //USART Receive Interrupt Flag bit(0=buffer empty/1= buffer full)
RCIE = 1; //USART Receive Internet Enable bit(C=disable / l=enable)
TXIE = 0; //USART Transmit Interrupt Enable bit(0=disable / l=enable)
PEIE = 1; //Peripheral Interrupt Enable bit(0=disable / l=enable)
GIE = 1; //Global Interrupt Enable bit(0=disable / l=enable)
}
//-------------------------------------发送 1 Byte
//参数:发送的数据
void sio_tx(char dat)
{
tx_buf[tx_cnta++] = dat;
if(tx_cnta >= TXBUFSIZE) tx_cnta = 0; //rotation ringbuff
if(TXIE==0)
{ //TXIE=0
TXREG = tx_buf[tx_cntb++];
}
if(tx_cntb >= TXBUFSIZE) tx_cntb=0; //rotation ringbuff
TXIE = 1; //USART Transmit Interrupt Enable bit
TXIF = 0; //USART Transmit Interrupt Flag bit
}
//检测发送状态
//返回值:0=发送完/1=发送中
char sio_txcheck(void)
{
return(TXIE);
}
//发送BCD码
//参数:发送的数据
void sio_txnum(int x)
{
char ch;
char enable=0;
int ketaval=100;
if(x<0)
{
x = -x;
sio_tx('-');
}
while(ketaval)
{
ch = (x / ketaval) % 10;
if((ch>0)||(ketaval==1)) enable=1;
if(enable)
{
sio_tx('0'+ ch);
}
ketaval /= 10;
}
}
//----------发送字符串
//参数:字符串的指针,结束符为0=NULL
void sio_txstr (char *str)
{
unsigned char tx_cnta2;
while(*str != 0)
{
tx_cnta2 = tx_cnta+1;
if(tx_cnta2 >= TXBUFSIZE) tx_cnta2 = 0; //rotation ringbuff
if(tx_cnta2 == tx_cntb)
{
while(sio_txcheck())
sio_tx(*str++);
}
}
}
此帖出自
小平头技术问答
一周热门 更多>