⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interface.c

📁 基于MEGA8单片机的AVR单片机STK500编程器源代码(GCC编译器),改软件完全实现ATMEL STK500协议
💻 C
字号:


#include <avr/io.h>
#include "tools.h"
#include "interface.h"

#include "config.h"

unsigned char checksum;

#ifdef CONFIG_INTERFACE_USB

//Hardware
#define	USB_TXE		(PINB&BIT6_POS)
#define	USB_RXF		(PINC&BIT3_POS)
#define	USB_RD_ON	PORTC&=BIT1_NEG
#define	USB_RD_OFF	PORTC|=BIT1_POS
#define	USB_WR_ON	PORTC|=BIT2_POS
#define	USB_WR_OFF	PORTC&=BIT2_NEG

/**
	Initializes the usb interface
*/
void interface_init(void)
{
  PORTC |= BIT1_POS;			//USB_RD inactive
  DDRC |= BIT1_POS|BIT2_POS;	//USB_RD, USB_WR as output
}

/**
	Reads a byte from the USB interface
	
	@return received character or -1 if no character has been received
*/
signed int interface_getc(void)
{
  unsigned char t;

  if (!USB_RXF)
    {
      USB_RD_ON;
      asm("NOP");
      t = PIND;
      USB_RD_OFF;

      return t;
    }
  else return -1;
}

/**
	Writes a byte to the USB
	
	@param t Byte to be written	
*/
void interface_putc(unsigned char t)
{
  while (USB_TXE);

  //PORTD auf Ausgang
  DDRD = 0xFF;
  //Daten auf den Port
  PORTD = t;

  USB_WR_ON;
  USB_WR_OFF;

  //PORTC auf Eingang
  DDRD = 0x00;
  //Pull-Ups ein
  PORTD = 0xFF;

  checksum ^= t;
}

#endif

#ifdef CONFIG_INTERFACE_RS232

#define USART_BAUD_RATE 115200   
#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16l)-1)

/**
	Initializes the rs232 interface
	
	Autor: Lukas Salzburger
*/
void interface_init(void)
{
  UCSRB =  (1<<RXEN) | (1<<TXEN);
  UBRRL = (unsigned char) USART_BAUD_SELECT;
  UBRRH = (unsigned char) (USART_BAUD_SELECT>>8);
}

/**
	Reads a byte from the UART interface
	
	Autor: Lukas Salzburger
	
	@return received character or -1 if no character has been received
*/
signed int interface_getc(void)
{

  if (UCSRA & (1<<RXC))
    {
      return UDR;
    }
  else return -1;
}

/**
	Writes a byte to the UART
	
	Autor: Lukas Salzburger
	
	@param t Byte to be written	
*/
void interface_putc(unsigned char t)
{

  while (!(UCSRA & (1<<UDRE)));

  UDR = t;
  checksum ^= t;
}

#endif

/**
	Writes a string to USB

	@param s The string to be send
*/
void interface_print(unsigned char *s)
{
  while (*s)
    {
      interface_putc(*s);
      s++;
    }
}

/**
	Sends a 16 bit value over the USB
	MSB first

	@param t Value to be send
*/
void interface_put16(unsigned int t)
{
  interface_putc((t>>8)&0xFF);
  interface_putc((t>>0)&0xFF);
}

/**
	Resets the checksum accumulator
*/
void interface_reset_check(void)
{
  checksum=0;
}

/**
	Sends the accumulated checksum
*/
void interface_send_check(void)
{
  interface_putc(checksum);
  checksum=0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -