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

📄 link1.c

📁 这个程序用于单片机和无线传输芯片CC2500之间的传送
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// F320_optics.c for 景老师,光学准直仪器
//-----------------------------------------------------------------------------
// Copyright 2008 USST Optics & Electronics Laboratories.
// Writer:Zhang Xuedian
// E-mail:zhangxuedian@hotmail.com
// Tel: 021-55271048
// Program Description:
//
// This program demonstrates how to configure the C8051F320 to use routines
// in STDIO.h to write to and read from the UART interface.  The program
// reads a character using the STDIO routine getkey(), outputs that character
// to the screen, and then outputs the ASCII hex value of that character.

// FID:            32X000088
// Target:         C8051F32x
// Tool chain:     Keil C51 8.12 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (PD)
//    -14 July, 2008
  
//2009.2.8

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f320.h>                 // SFR declarations
#include <stdio.h>
#include <stdlib.h>
#include <intrins.h>
#include <absacc.h>


#define FLOG_USE_FLASH;

volatile bit gbTmr2Flag = 0;

void Delay(unsigned int MS);
unsigned char ReadStringUART(char *Dest,unsigned char BufferLen);

#ifdef FLOG_USE_FLASH
   #define FLASH_PAGESIZE        512
   #define FLOG_PAGE_ADDR       0x03A00L // last usable flash page
  // #define MAX_USER_RESPONSE_LEN   20
#endif
 

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK      12000000           // SYSCLK frequency in Hz
#define BAUDRATE       9600           // Baud rate of UART in bps
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------
//CA12864K 串行C8051通讯

//***************************************************************************
//连线表:                          *
//SCK=P0.0       SID=P0.1     Reset=RC in Board   CS=not used*
//***************************************************************************

sbit SCK=P0^0;
sbit SID=P0^1;

sbit    sel0=P1^0;
sbit	sel1=P1^1;
sbit	xReady=P1^2;// input
sbit	yReady=P1^3;//define pin as input mode
sbit    LampXY=P1^4;//input

#define LampX       1
#define LampY       0 


//按键连接到p1.5、p1.6、p1.7均为开漏输输入
sbit	key1=P1^5;// SW1 ='0' means switch pressed
sbit	key2=P1^6;
sbit	key3=P1^7;

//积分时间,output
sbit    steps0=P0^6;
sbit    steps1=P0^7;

//-----------------------------------------------------------------------------
unsigned char code AC_TABLE[]={
		0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,     //第一行汉字
		0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,     //第二行汉字
		0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,     //第三行汉字
		0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f };   //第四行汉字

//unsigned char code str1[]= "Welcome Optics!";


//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//------------------------------------0-----------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
// This function configures the crossbar and ports pins, GPIO ports
// 
// To configure a pin as a digital input, the pin is configured as digital
// and open-drain and the port latch should be set to a '1'.  The weak-pullups
// are used to pull the pins high.  Pressing the switch pulls the pins low.
//
// To configure a pin as a digital output, the pin is configured as digital
// and push-pull.  
//
// An output pin can also be configured to be an open-drain output if system
// requires it.  For example, if the pin is an output on a multi-device bus,
// it will probably be configured as an open-drain output instead of a 
// push-pull output.  For the purposes of this example, the pin is configured
// as push-pull output because the pin in only connected to an LED.
//作为数字输入采用开漏形式,先将端口寄存器值"1",然后在读取端口状态.
//作为数字输出采用推挽形式,且内部弱上拉,如果外部有上拉电阻,需要配置成开漏形式

// SCLK=P0.0       SID=P0.1 
// P0.0   digital   open-drain    SCK OUTPUT 推挽模式
// P0.1   digital   open-drain    SID  开漏且弱上拉,读之前,先写1

// P0.2   digital   open-drain    XPLUSE2  OUTPUT 推挽模式
// P0.3   digital   open-drain    YPLUSE2  开漏且弱上拉,读之前,先写1
//
// 
// P0.4   digital   push-pull    UART TX   OUTPUT
// P0.5   digital   open-drain   UART RX   INPUT
// 
// P0.6   digital   push-pull    INTE TIME   OUTPUT
// P0.7   digital   push-pull   INTE TIME   OUTPUT
//------------------------------------------------------------
// P1.0-1   digital   push-pull    sel  OUTPUT
// P1.3		digital   open-drain    READY  BI
// P1.4     digital   open-drain   Lamp   BI

// P1.5-7   digital   push-pull    KEYS   INPUT
//-----------------------------------------------------------

// P2   digital   open-drain   DATA IN   INPUT

//-----------------------------------------------------------------------------

void PORT_Init (void)
{
   P0MDOUT |= 0x10;                    // enable UTX as push-pull output
   XBR0    = 0x01;                     // Enable UART on P0.4(TX) and P0.5(RX)                     
     
   P0MDIN |= 0xff;                     // Lower three pins on P0 are digital
   P0MDOUT |= 0xff;                    // Enable LEDs as push-pull outputs=0
                                       // Enable Switches as open-drain=1
   P0     |= 0x03;                     // Set port latches for P0.0 and P0.1 to '1'

   steps0=1;
   steps1=1;

 // btn pins and ext clk pin are inputs, set high impedance
   P1MDIN |= 0xFF;                     // pins on P1 are digital INPUT
   P1MDOUT |= 0x00;                    // Open-drain
   P1     |=0xE0;                      // first write '1' before read p2.key1,key2,key3
                                       //Set port latches for P2 to '1'
      

   P2MDIN |= 0xFF;                     // pins on P2 are digital INPUT
   P2MDOUT |= 0x00;                    // Open-drain
   P2=0xFF;                            // first write '1' before read p2.
                                       //Set port latches for P2 to '1'

   XBR1    = 0x40;                     // Enable crossbar and enable
                                       // weak pull-ups
}

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine initializes the system clock to use the internal oscillator
// at its maximum frequency.
// Also enables the Missing Clock Detector.
//-----------------------------------------------------------------------------

void SYSCLK_Init (void)
{
	OSCICN |= 0x83;                     // Configure internal oscillator for
	                                   // its maximum frequency
	CLKSEL = 0x20;
	RSTSRC  = 0x04;                     // Enable missing clock detector

	TCON      = 0x40;
	TMOD      = 0x22;
	CKCON     = 0x55;
/*
	TMR3CN    = 0x04;
	TMR3RLL   = 0x68;
	TMR3RLH   = 0xC5;
	TMR3L     = 0xFF;
	TMR3H     = 0xFF;*/


}
/*
void Tmr3_ISR(void) interrupt INTERRUPT_TIMER3
 {
   TMR3CN &= ~0x80;
   gnTimeTick++;
   gnSecTick++;
   if (gnSecTick == 100)
      {
      gnSecTick = 0;
      gnSec++;
      if (gnSec == 60)
         {
         gnSec = 0;
         gnMin++;
         if (gnMin == 60)
            {
            gnMin = 0;
            gnHrs++;
            if (gnHrs == 24)
               gnHrs = 0;
            }
         }      
      }
   }
*/


//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//-----------------------------------------------------------------------------

void UART0_Init (void)
{
   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        ninth bits are zeros
                                      //        clear RI0 and TI0 bits
   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
      CKCON |=  0x01;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   }
   
	//TH1 = -156;                   //(SYSCLK/BAUDRATE/2/4)=-156.25;
//	CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01,9600, 2.44                 
//	CKCON |=  0x01;

   TL1 = TH1;                          // init Timer1
   TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
   TMOD |=  0x20;                       
   TR1 = 1;                            // START Timer1
   TI0 = 1;                            // Indicate TX0 ready
}


//------------------------------------------------------------------------
//LCM 液晶CA12864K 串行方式
//------------------------------------------------------------------------
//串口发送一个字节
void SendByte(unsigned char Dbyte)
{
  unsigned char i;
  for(i=0;i<8;i++)
  {
      SCK = 0;
      Dbyte=Dbyte<<1;     //左移一位
      SID = CY;         //移出的位给SID
	  SCK = 1; 
      SCK = 0;
  }
}

//串口从LCD接受一个字节;仅在读取数据的时候用到
//而读出得数据一次只能读出4bit
/*
unsigned char ReceiveByte(void)
{
  unsigned char i,temp1,temp2;
  temp1=temp2=0;
  for(i=0;i<8;i++)
  {
      temp1=temp1<<1;
      SCK = 0;
      SCK = 1;         
      SCK = 0;
      if(SID) temp1++;
  }
  for(i=0;i<8;i++)
  {
      temp2=temp2<<1;
      SCK = 0;
      SCK = 1;
      SCK = 0;
      if(SID) temp2++;
  }
  return ((0xf0&temp1)+(0x0f&temp2));
}

void CheckBusy( void )
{
  do   SendByte(0xfc);     //11111,RW(1),RS(0),0
  while(0x80&ReceiveByte());     //BF(.7)=1 Busy
}
*/
void WriteCommand( unsigned char Cbyte )
{
  //CS = 1;
//  CheckBusy();
  Delay(1);
  SendByte(0xf8);         //11111,RW(0),RS(0),0
  SendByte(0xf0&Cbyte);     //高4位
  SendByte(0xf0&Cbyte<<4);//低4位(先执行<<)
  //CS = 0;
}

void WriteData( unsigned char Dbyte )
{
  //CS = 1;
//  CheckBusy();
  Delay(1);
  
  SendByte(0xfa);         //11111,RW(0),RS(1),0
  SendByte(0xf0&Dbyte);     //高4位
  SendByte(0xf0&Dbyte<<4);//低4位(先执行<<)
  //CS = 0;
}
/*
unsigned char ReadData( void )
{
  CheckBusy();
  SendByte(0xfe);         //11111,RW(1),RS(1),0
  return ReceiveByte();
}*/


void LcmInit( void )
{
  WriteCommand(0x30);     //8BitMCU,基本指令集
  WriteCommand(0x03);     //AC归0,不改变DDRAM内容
  WriteCommand(0x0C);     //显示ON,游标OFF,游标位置反白OFF
  WriteCommand(0x01);     //清屏,AC归0
  WriteCommand(0x06);     //写入时候,游标右移动
  
}

//图形区和文本区显示在2个不同的RAM区
//文本区清RAM函数
void LcmClearTXT( void )
{
  unsigned char i;
  WriteCommand(0x30);     //8BitMCU,基本指令集
  WriteCommand(0x80);     //AC归起始位
  for(i=0;i<64;i++)
    WriteData(0x20);       //0x20相同""
}


//写字符串
void PutStr(unsigned char row,unsigned char col,unsigned char *puts)
{
  WriteCommand(0x30);     //8BitMCU,基本指令集
  WriteCommand(AC_TABLE[8*row+col]);     //起始位置
  while(*puts != '\0')     //判断字符串是否显示完毕
  {
      if(col==8)         //判断换行
       {         //若不判断,则自动从第一行到第三行
          col=0;
          row++;
       }
      if(row==4) row=0;     //一屏幕显示完,回到屏左上角
      WriteCommand(AC_TABLE[8*row+col]);
      WriteData(*puts);     //一个汉字要写两次
      puts++;
      WriteData(*puts);
      puts++;
      col++;
  }
}
/*
//图形区清RAM函数
void LcmClearBMP( void )
{
  unsigned char i,j;
  WriteCommand(0x34);         //8Bit扩充指令集,即使是36H也要写两次
  WriteCommand(0x36);         //绘图on,基本指令集里边36H不能开绘图
  for(i=0;i<32;i++)           //12864实际为256x32
  {
      WriteCommand(0x80|i);   //行位置
      WriteCommand(0x80);     //列位置
      for(j=0;j<32;j++)       //256/8=32 byte
          WriteData(0);
  }
}
//绘图函数
void PutBMP(unsigned char *puts)
{
  unsigned int x=0;
  unsigned char i,j;
  WriteCommand(0x34);     //8Bit扩充指令集,即使是36H也要写两次
  WriteCommand(0x36);     //绘图on,基本指令集里边36H不能开绘图
  for(i=0;i<32;i++)         //12864实际为256x32
  {
      WriteCommand(0x80|i);     //行位置
      WriteCommand(0x80);     //列位置
      for(j=0;j<32;j++)     //256/8=32 byte
      {         //列位置每行自动加1
          WriteData(puts[x]);
          x++;
      }
  }
}

*/
//----------------------------------------------------------------------------


void Delay(unsigned int MS)
{
  unsigned char us,usn;
  while(MS!=0)         //for 12M
      { usn = 2;
          while(usn!=0)
              {
                  us=0xf5;
                  while (us!=0){us--;};
                  usn--;
              }
          MS--;
      }
}

// flash_ErasePage
// This routine erases the FLASH page containing the linear FLASH address <addr>.

void FlashErasePage (unsigned char xdata * pwrite)
 {
   bit EA_SAVE = EA;                   // preserve EA
   //char xdata * data pwrite;           // FLASH write pointer
         
   EA = 0;                      // disable interrupts
   // change clock speed to slow, then restore later
   VDM0CN = 0x80;                      // enable VDD monitor
   RSTSRC = 0x02;                   // enable VDD monitor as a reset source
   //pwrite = (char xdata *) addr;
   FLKEY  = 0xA5;                   // Key Sequence 1
   FLKEY  = 0xF1;                   // Key Sequence 2
   PSCTL |= 0x03;                   // PSWE = 1; PSEE = 1
   VDM0CN = 0x80;                      // enable VDD monitor
   RSTSRC = 0x02;                   // enable VDD monitor as a reset source
   * pwrite = 0;                     // initiate page erase
   PSCTL &= ~0x03;                  // PSWE = 0; PSEE = 0
   EA = EA_SAVE;                    // restore interrupts
 }


//Flash 读写操作
//pwrite为FLASH地址指针,采用xdata强制指明指针类型,以保证采用movx操作

⌨️ 快捷键说明

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