PIC10上最小(代码方式)位 - 带状串行接口

时间:2015-02-05 06:37:58

标签: pic uart i2c spi

我需要通过一些串行接口连接到PIC10 micro。由于这些小型器件缺乏对SPI I2C和UART的硬件支持,因此软件解决方案是不可避免的。

但是,由于我需要保留尽可能多的Programm内存来存储(静态)配置并识别要通过所述接口检索的信息,这可能是最小的解决方案?

我需要在ASM中对此进行编程,因为PIC10似乎没有好的C编译器。但是,这将是我第一次真正与ASM见面。

2 个答案:

答案 0 :(得分:0)

尝试包含代码示例的this download并说:

"下面列出了五个PIC UART软件例程,用于没有硬件UART的PIC微处理器"

链接已损坏原始链接尝试this link

答案 1 :(得分:0)

在我看来,xc8编译器也很好用,对于PIC10也是如此。 这是C语言中的示例:

#include <xc.h>
#ifndef _XTAL_FREQ
    #define _XTAL_FREQ 4000000
#endif

#define Baudrate 1200 //bps
#define OneBitDelay (1000000/Baudrate)
#define DataBitCount 8 // no parity, no flow control
#define UART_RX RA1 // UART RX pin
#define UART_TX RA0 // UART TX pin
#define UART_RX_DIR TRISA1 // UART RX pin direction register
#define UART_TX_DIR TRISA0 // UART TX pin direction register

//Function Declarations
void InitSoftUART(void);
unsigned char UART_Receive(void);
void UART_Transmit(const char);

__CONFIG(FOSC_INTOSC & MCLRE_OFF & WDTE_OFF & LVP_OFF & CP_OFF &
         WRT_OFF & PWRTE_OFF & WRT_OFF & BOREN_ON & LPBOR_ON & BORV_LO);

void main()
{ 
 unsigned char ch = 0;

 ANSELA = 0x00; // Set ports as digital I/O, not analog input
 ADCON = 0x00; // Shut off the A/D Converter
 FVRCON = 0x00; // Shut off the Voltage Reference
 PORTA = 0x00; // Make all pins 0

 InitSoftUART(); // Intialize Soft UART

 InitSoftUART(); // Intialize Soft UART

 while(1)
 {
  ch = UART_Receive(); // Receive a character from UART
  UART_Transmit(ch); // Echo back that character
 }
}


void InitSoftUART(void) // Initialize UART pins to proper values
{
 UART_TX = 1; // TX pin is high in idle state

 UART_RX_DIR = 1; // Input
 UART_TX_DIR = 0; // Output
}

unsigned char UART_Receive(void)
{
 // Pin Configurations
    // GP1 is UART RX Pin

 unsigned char DataValue = 0;

 //wait for start bit
 while(UART_RX==1);

 __delay_us(OneBitDelay);
 __delay_us(OneBitDelay/2); // Take sample value in the mid of bit duration

 for ( unsigned char i = 0; i < DataBitCount; i++ )
 {
  if ( UART_RX == 1 ) //if received bit is high
  {
   DataValue += (1<<i);
  }

  __delay_us(OneBitDelay);
 }

 // Check for stop bit
 if ( UART_RX == 1 ) //Stop bit should be high
 {
  __delay_us(OneBitDelay/2);
  return DataValue;
 }
 else //some error occurred !
 {
  __delay_us(OneBitDelay/2);
  return 0x000;
 }
}

void UART_Transmit(const char DataValue)
{
 /* Basic Logic

    TX pin is usually high. A high to low bit is the starting bit and 
    a low to high bit is the ending bit. No parity bit. No flow control.
    BitCount is the number of bits to transmit. Data is transmitted LSB first.

 */

 // Send Start Bit
 UART_TX = 0;
 __delay_us(OneBitDelay);

 for ( unsigned char i = 0; i < DataBitCount; i++ )
 {
  //Set Data pin according to the DataValue
  if( ((DataValue>>i)&0x1) == 0x1 ) //if Bit is high
  {
   UART_TX = 1;
  }
  else //if Bit is low
  {
   UART_TX = 0;
  }

     __delay_us(OneBitDelay);
 }

 //Send Stop Bit
 UART_TX = 1;
 __delay_us(OneBitDelay);
}

此代码的来源是:Microchip forum