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

📄 main.c

📁 很经典的单片机程序
💻 C
字号:
#include <XC167.h>
#include <stdio.h> 
#include <stdlib.h>  
#include <absacc.h> //宏定义,可以对某个绝对地址的存储器直接访问
#include <Intrins.h>//内联函数
#include <math.h>
#include <main.h> 

//DPR    起始地址  0x00 0000  空间  16KByte   CS0
//FPGA   起始地址  0x01 0000  空间  64KByte   CS1
//Flash  起始地址  0x10 0000  空间  1MByte	  CS2
//SRAM   起始地址  0x08 0000  空间  512KByte  CS3

//DPR 0x00 1FFF: CPU side interrupt mailbox
//DPR 0x00 1FFE:  X6 side interrupt mailbox
//可以读双方的interrupt box,只能写对方的interrupt box
#define DPR MARRAY(int, 0x000000)
#define SEM MVAR(int, 0x000000)
#define DPR_X6Mailbox  MVAR(unsigned char, 0x003FFE)
#define DPR_CPUMailbox MVAR(unsigned char, 0x003FFF) 

#define FPGA_TX MARRAY(int, 0x010000)//TX 32K
#define FPGA_RX MARRAY(int, 0x018000)//RX 32K

unsigned char buffer[15];

void Delay(unsigned int US) 
{
	while(US!=0)
	  US--;
}

//初始化系统
void init(void)
{
  //Port9 Data Register 
  	P9  = 0x03;    
  //Port9 Direction Ctrl. Register: 0 input, 1 output
  	DP9 = DP9|0x03;
  
  //Port2 输入为标准TTL电平  
  	PICON_P2HIN = 0;
  	//Port2 Direction Ctrl. Register: 0 input, 1 output
  	//P2_13: P1_X/INT-EV  Input
  	//P2_14: P1_X/INT-CI  Input
  	//P2_15: D1_INT       Input
  	DP2 = DP2 & 0x1F;

  //Port 7 P7_P4: SEM DPR 旗语逻辑,设定为输出
  //P7_P4  = 1;  
  //DP7 = DP7 | 0x10;   
}

/*------------CRC16校验码的产生与校对-------------*/
//对buffer[0]~buffer[dataLength-1]的字符计算CRC16校验码 
//check=1 => 用生成的校验码与buffer[dataLength]和buffer[dataLength+1]进行比较,
//判断接收到的CRC16与本程序生成的校验码是否一致
//check=0 => 用生成的校验码写入buffer[dataLength]和buffer[dataLength+1]中,
//发送给接收设备
/*-------------------------------------------------*/
bit CRC16(unsigned int dataLength,unsigned char check)
{
	unsigned int CheckSum;
	unsigned int j;
	unsigned char lowCRC;
	unsigned char highCRC;
	unsigned short i;
	
	CheckSum = 0xFFFF;
	for (j=0; j<dataLength; j++)
	{
		CheckSum = CheckSum^(unsigned int)buffer[j];
		for(i=8; i>0; i--)
		{
		   //MODBUS 生成多项式0xA001: x^16+x^15+x^14+1
		   //PROFIBUS 生成多项式 0x1DCF:x^16+x^12+x^11+x^10+x^8+x^7+x^6+x^3+x^2+x+1
			if(CheckSum & 0x0001)
				CheckSum = (CheckSum>>1)^0xA001;
			else
				CheckSum >>= 1;
		}	
	}
	highCRC = (unsigned char)(CheckSum>>8);
	CheckSum =CheckSum <<8;
	lowCRC = (unsigned char)(CheckSum >>8);
	if(check==1)
		{
			if((buffer[dataLength+1]==highCRC) &(buffer[dataLength]==lowCRC))
				return 1;
			else return 0;	
		} 
	else
		{
			buffer[dataLength] = lowCRC;
			buffer[dataLength+1] = highCRC;
			return 1; 	
		} 
}

void main (void)
{
 	unsigned char dataRX[16];
	int temp;
	int i,data[16];

	init();
	while (1) 
	{
		if(P2_P14==0)
	   	{
	  	   //向FPGA写数据:无数据域固定长度的帧:(SYN=33bit) SD1=10H  DA SA FC FCS1 ED=16H 
			FPGA_TX[0] = SD1;
			FPGA_TX[1] = 0xAB;
			FPGA_TX[2] = 0x19;
			FPGA_TX[3] = 0x20;
			FPGA_TX[4] = 0xE4;
			FPGA_TX[5] = ED;
		}
		if(P2_P13==0)	  //从FPGA接收数据
        {
		    for (i =0; i<4; i++)
			{
			    temp = FPGA_RX[i];
			    dataRX[i*2]  = (unsigned char)(temp  & 0x00FF); 
			    dataRX[i*2+1]= (unsigned char)(temp>>8 & 0x00FF);
			}
    	   	P9  = ~P9;  
	    	Delay(10000);	   
        }
/*------------采用旗语方式对DPR操作-------------*/
	  //申请独占DPR,SEM = 0;--->> 使能P7_P4 SEM标志位 
//      P7_P4  = 0;  
//      DP7    = DP7 | 0x10;
//      SEM    = 0;
//      _nop_();

	   //回读SEM设置,如果为0,申请独占成功,可进行读写操作
//      if (SEM ==0)
//     {
//  		DPR[0]= 0xAB;
//	      	DPR[1]= 0x54;
//	      	DPR[2]= 0x36;
//	      	DPR[3]= 0x89;		 
//
//			//释放独占权
//			SEM = 1;
//	  		P7_P4  = 1;  
//	   		DP7 = DP7 | 0x10; 
//	   	}
/*------------采用中断方式对DPR操作----------------*/
	   if(P2_P15==0)
	   {
	   	//	if(DPR_CPUMailbox==0x11)  
	   		DPR[0]= 0xAB;
	      	DPR[1]= 0x54;
	      	DPR[2]= 0x36;
	      	DPR[3]= 0x89;	
	   }
        for (i =0;i<4;i++)
	    	data[i]=DPR[i];
	
   }
}

⌨️ 快捷键说明

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