打印

c8051f310单片机整口读问题?

[复制链接]
1184|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xu0801hua|  楼主 | 2014-7-31 09:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

#include "c8051f310.h"
//#include "compiler.h"                // SFR declarations
//sfr P0SKIP   =  0xd4;
//sfr EIE1       =  0xe6;
//sfr CP0RIF   = ;
unsigned int num=0,pj=0,wb=0,x=10;
unsigned int umn=0,jp=0,ke=0;
//bit ;
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------

sbit SW1  =  P0^7;                     // SW1 ='0' means switch pressed
sbit LED1 =  P0^7;                     // LED1 ='1' means ON

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void OSCILLATOR_Init (void);           
void PORT_Init (void);
void Oscillator_Inits(void);

void delayms(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
   for(j=330;j>0;j--);
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and ports pins.
//
// 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.
//
// P0.7   digital   open-drain    Switch 1
// P3.3   digital   push-pull     LED1
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   P0MDIN &= ~0x0C;                    // P0.2, P0.3 are analog  
   P0SKIP |= 0x0C;                     // P0.2, P0.3 skipped in the Crossbar
   P0MDOUT = 0x11;                     // P0.7 is open-drain
   P0     |= 0x80;                     // Set P0.7 latch to '1'
   
   P1MDIN  &= ~0x03;                   // Comparator 0 inputs (P1.0 and P1.1)
   P1SKIP |= 0x03;                                    // are analog inputs.
   
   P2MDIN  =0XFF;               //P2口全部数字输入
//   P2MDOUT = 0xff;                         //P2口全部推挽输出
//   P2=0xff;                                         //P2口低4位设置高电平,高4位设置低电平
//   P2SKIP |= 0x00;                         //P2口都不跳过
     
  // P3MDIN |= 0x08;                     // P3.3 is digital   
   P3MDOUT = 0x08;                     // P3.3 is push-pull
   
   XBR1    = 0x60;
   XBR0    = 0X10;                     // Enable crossbar and enable
                                       // weak pull-ups
}
//-----------------------------------------------------------------------------
// Timer0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the Timer0 as a 8-bit timer with reload, interrupt
// enabled.
// Using the internal osc. at 24.5MHz with a prescaler of 1:8 and reloading
// TL0 register with TH0 it will interrupt aproximately one time every 1msec.
//
// Note: The Timer0 uses a 1:48 prescaler.  If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer0_Init(void)
{  
   EA=1;

   TH0 = (65536-15360)/256;           // Init Timer0 High register
   TL0 = (65536-15360)%256;                          // Set the intial Timer0 value

   TL1 = (65536-15360)/256;//0x00;
   TH1 = (65536-15360)%256;//0X00;

   TMOD = 0x11;                        // Timer0 in 16-bit reload mode
//  CKCON |= 0x04;                    // Timer0 uses a 1:48 prescaler
   ET0 = 1;                            // Timer0 interrupt enabled
   ET1 = 1;
//   TCON = 0x10;                        // Timer0 ON
   TR0 = 1;
   TR1 = 1;

   EIE1= 0X20;
}

/*****************定时c秒函数************************/
        void deng()
        {
        if(pj==x)//判断标记位
   {
     pj=0;  
         wb=TH1*255+TL1;//读计数器数据
//     led=1;
     LED1=0;
     TR0=0;
         TR1=0;
        }
        }


//-----------------------------------------------------------------------------
// Comparator0_Init
//
// Return Value : None
// Parameters   : None
//
//  This function configures the comparator to pins P1.0 and P1.1
//-----------------------------------------------------------------------------

void Comparator0_Init (void)
{
        unsigned int i;
    CPT0CN    = 0x8f;
        CPT0MX    = 0x00;                  // P1.1 = Negative Input  
                                       // P1.0 = Positive Input
    for (i = 0; i < 35; i++);  // Wait 10us for initialization
    CPT0CN   &= ~0x30;
//        CPT0MD    =  0x20;
}

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------

void main (void)
{
  unsigned int zi=0;
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                       // enable)
   PCA0MD    = 0x00;
   PORT_Init();                        // Initialize Port I/O
//   OSCILLATOR_Init ();                 // Initialize Oscillator
   Oscillator_Inits();
   Timer0_Init();
   Comparator0_Init();                 // Initialize Comparator0
   while (1)
   {
//   P2=0xfb;                                         //P2口低4位设置高电平,高4位设置低电平
    P2=~P2;  
    ke=P2;
        switch(ke)
        {          
        case 0x01:
                     for(zi=1;zi>0;zi--)
                    {
                    LED1=0;delayms(1000);LED1=1;
                        }
                        break;

        case 0x02:  
                         for(zi=2;zi>0;zi--)
                    {
                    LED1=0;delayms(1000);LED1=1;
                        }
                    break;

        case 0x03:  
                         for(zi=3;zi>0;zi--)
                    {
                    LED1=0;delayms(1000);LED1=1;
                        }
                   break;

        case 0x04:  
                          for(zi=4;zi>0;zi--)
                    {
                    LED1=0;delayms(1000);LED1=1;
                        }
                  break;

        case 0x05:  
                         for(zi=5;zi>0;zi--)
                    {
                    LED1=0;delayms(1000);LED1=1;
                        }
                  break;
         
        }
//   if((CPT0CN&0x40)==0)
//   {
////    delayms(30);
////        if(CPT0CN==0X8F)
////        {
//    LED1=0;
////        }
// //   while(1);
//   }
//   else LED1=1;        
   }                                   // end of while(1)
}                                      // end of main()

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

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use the internal oscillator
// at its maximum frequency.
//
//-----------------------------------------------------------------------------
void Oscillator_Inits(void)                    //外部晶振初始化 3.6864M
{
     unsigned int i;
     OSCXCN=0x65;                 //start external oscillator with
                                  // the appropriate XFCN setting
                                  // based on crystal frequency
     for (i=0;i<3000;i++);                    // 等1mS延时
     while (!(OSCXCN&0x80));
     RSTSRC = 0X06;
         CLKSEL=0x01;
     OSCICN=0x00;
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer0_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer0 interrupt and toggle the LED when appropriate
//
//-----------------------------------------------------------------------------
void Timer0_ISR (void) interrupt 1
{
//   static int counter = 0;
//
//   if((counter++) == LED_TOGGLE_RATE)
//   {
//      LED = ~LED;                      // Toggle the LED
//      counter = 0;
//   }
   TH0 = (65536-15360)/256;           // Init Timer0 High register
   TL0 = (65536-15360)%256;
   num++;
   if(num==20)
   {
    num=0;
        pj++;
//        LED1=~LED1;
   }
}

相关帖子

沙发
renqinglei| | 2014-7-31 09:16 | 只看该作者
什么问题?

使用特权

评论回复
板凳
茫茫大士| | 2014-7-31 09:33 | 只看该作者
同问

使用特权

评论回复
地板
ayb_ice| | 2014-7-31 09:42 | 只看该作者
有奖竞猜

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

34

主题

81

帖子

1

粉丝