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

📄 main.c

📁 Freescal MC9S08GT60的一些实例源码
💻 C
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC9S08GB60.h> /* include peripheral declarations */

#define Vrefh 330 /* Define Reference Voltage High */
#define Vrefl 0 /* Define Reference Voltage Low */

char Receive_SCI_Byte();
void Transmit_SCI_Byte(char T_Byte);
void Transmit_SCI_String(const char *pString);
void Out1Num(uchar src);
void OutHex(uchar src);

void main(void) {
	DisableInterrupts;  /* Disable Interrupts */
  
	/* Initial_ICG */
	ICGC1 = 0x78;
	ICGC2 = 0x30;	/* Enable the External Crystal = 4 MHz, Bus Frequency = 20 MHz */
	  
  /* Initial_SCI1 */
	SCI1BDL = 0x82;	/* Set Baud Rate for 20MHz Bus Frequency */
	SCI1C2 = 0x2C;	/* Enalbe SCI R/W and enable Receiver Interrupt */
  
  /* Initial ATD */
  ATD1C = 0xC4; /* 10 bits ATD functionality unsigned right justified */
  ATD1SC = 0x00;  /* Set single conversion mode and enable ATDCH0 */
  ATD1PE = 0x01;  /* Enable ATDCH0(PTB0) */
  
  PTADD = 0xFF; /* Set Port A as output */
  
	asm{
		SEI
	}
	
  EnableInterrupts; /* enable interrupts */

  for(;;) {
    __RESET_WATCHDOG(); /* feeds the dog */
  } /* loop forever */
}

/* Reveive a byte from SCI */
char Receive_SCI_Byte(){
	while((SCI1S1&0x20) == 0) __RESET_WATCHDOG();
	return SCI1D;
}

/* Transmit a byte to SCI */
void Transmit_SCI_Byte(char T_Byte){
	while((SCI1S1&0x80) == 0) __RESET_WATCHDOG();
	SCI1D = T_Byte;
}

/* Transmit string to SCI */
void Transmit_SCI_String(const char *pString){
	char *pTemp = pString;
	while(*pTemp != 0) {
	  Transmit_SCI_Byte(*pTemp++);
	  __RESET_WATCHDOG();
	}
}

/* Transform src value(0-9 & A-F) to ASCII code and transmit to SCI */
void Out1Num(uchar src) {
	src += '0';
	if(src >'9') src += 'A'-'0'-10;
	Transmit_SCI_Byte(src);
}

/* Transform src to two ASCII codes and transmit to SCI */
void OutHex(uchar src) {
	Out1Num(src / 16);
	Out1Num(src % 16);
}

interrupt SCI(void) {
  char value_H;
  char value_L;
  char Temp;
  long int value;
  Temp = Receive_SCI_Byte();
  /* Character 'a' and 'A' startup AD conversion */ 
  if((Temp == 'a') || (Temp == 'A')) {
    ATD1SC = 0x00;
    while((ATD1SC & 0x80)==0) __RESET_WATCHDOG();
    value_H = ATD1RH;
    Transmit_SCI_String("\r\nData:");
    OutHex(value_H);
    value_L = ATD1RL;
    PTAD = ~value_L;  /* Send the conversion value to Port A and change the state of LED in the OUT module */
    OutHex(value_L);
    Transmit_SCI_String("  Voltage:");
    /* Transform Hex value to Dec and transmit to SCI */
    value = (int)value_H * 256;
    value = (int)value_L +value;
    value = (value * (Vrefh - Vrefl)) / 1023;
    Out1Num((uchar)(value / 100));
    Out1Num((uchar)((value / 10) % 10));
    Out1Num((uchar)(value % 10));
  }
}
  

⌨️ 快捷键说明

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