打印

C8051F040出现重大问题

[复制链接]
4378|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
shell.albert|  楼主 | 2010-11-2 17:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我使用了C8051F040 MCU,在做串口编程时,屡屡失败,后来我就写了一个最基本的小程序,只更改SBUF0的值,一调试发现就是写不进值去,程序如下:

void main(void){
    WDT_Disable; /*disable watchdog*/
    SFRPAGE=0x0F;
    OSCICN=0x83; /*internal clock,24.5MHz*/
    SFRPAGE=0;
    SBUF0=0xFF;/*这里就是写不进值去,还是0x00*/
    while(1);
    ...         ........
}

相关帖子

沙发
shell.albert|  楼主 | 2010-11-2 17:03 | 只看该作者
我使用的是新华龙的C8051F040和它的仿真器,写别的寄存器是没有问题的,如下:
SFRPAGE=0;
SADDR0=0xFF; /*能写进去,没有任何问题*/

但是为什么写SBUF0就是不行,我发不出数据去啊?!

救命啊。。。

使用特权

评论回复
板凳
alamo2009| | 2010-11-2 18:20 | 只看该作者
你最好用示波器测一下,说不定等你停下来看的时候SBUF0的数据已经发出去了

使用特权

评论回复
地板
chunyang| | 2010-11-2 19:04 | 只看该作者
检查波特率发生器是否启动和正确配置。

使用特权

评论回复
5
shell.albert|  楼主 | 2010-11-2 20:26 | 只看该作者
我使用了Timer3作为UART0的波特率发生器,自动装载模式,并且启动了TR3,但是结果就是写不进值去.
示波器? 根本不用看,单步执行的时候卡在这里,示波器肯定显示不出来.

使用特权

评论回复
6
ayb_ice| | 2010-11-3 08:47 | 只看该作者
"
最好通知SILABS公司
说不定还能有个奖励什么的
"

使用特权

评论回复
7
yhn1973| | 2010-11-3 09:22 | 只看该作者
SBUF0读和写是对应于两个寄存器,读的是接收寄存器,写的是发送寄存器,IDE中看到的值应该是接收寄存器

使用特权

评论回复
8
yuhison| | 2011-4-8 16:22 | 只看该作者
请问你现在解决了吗?如何解决的?我也遇到这个问题了,真郁闷~~

使用特权

评论回复
9
xlsbz| | 2011-4-8 16:42 | 只看该作者
应该没问题啊 我天天用啊 挺好的啊  

要么你先禁止接收 然后 发送 然后允许接收试试

使用特权

评论回复
10
xlsbz| | 2011-4-8 16:46 | 只看该作者
//-----------------------------------------------------------------------------
// F04x_UART0_INT.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure the C8051F040 to write to and read
// from the UART interface. The program reads a word using the UART0 interrupts
// and outputs that word to the screen, with all characters in uppercase
//
// How To Test:
//
// 1) Download code to a 'F04x device that is connected to a UART transceiver
// 2) Verify jumpers J6 and J9 are populated on the 'F04x TB.
// 3) Connect serial cable from the transceiver to a PC
// 4) On the PC, open HyperTerminal (or any other terminal program) and connect
//    to the COM port at <BAUDRATE> and 8-N-1
// 5) Type up to 64 characters into the Terminal and press Enter.  The MCU
//    will then print back the characters that were typed
//   
//
// Target:         C8051F04x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (SM)
//    -10 JULY 2007
//


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

#include <C8051F040.h>                 // SFR declarations
#include <stdio.h>

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F04x
//-----------------------------------------------------------------------------

sfr16 RCAP2    = 0xCA;                 // Timer2 capture/reload
sfr16 TMR2     = 0xCC;                 // Timer2

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define BAUDRATE     115200            // Baud rate of UART in bps

// SYSTEMCLOCK = System clock frequency in Hz
#define SYSTEMCLOCK       22118400L

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

void OSCILLATOR_Init (void);
void PORT_Init (void);
void UART0_Init (void);

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

#define UART_BUFFERSIZE 64
unsigned char UART_Buffer[UART_BUFFERSIZE];
unsigned char UART_Buffer_Size = 0;
unsigned char UART_Input_First = 0;
unsigned char UART_Output_First = 0;
unsigned char TX_Ready =1;
static char Byte;

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

void main (void)
{
   SFRPAGE = CONFIG_PAGE;

   WDTCN = 0xDE;                       // Disable watchdog timer
   WDTCN = 0xAD;

   OSCILLATOR_Init ();                 // Initialize oscillator
   PORT_Init ();                       // Initialize crossbar and GPIO

   UART0_Init ();                      // Initialize UART0
   
   EA = 1;

   SFRPAGE = UART0_PAGE;   

   while (1)
   {  
      // If the complete word has been entered via the terminal followed
      // by carriage return
      if((TX_Ready == 1) && (UART_Buffer_Size != 0) && (Byte == 13))
      {
         TX_Ready = 0;                  // Set the flag to zero
         TI0 = 1;                       // Set transmit flag to 1
      }        
   }
}

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

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use an external 22.1184MHz
// crystal.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
   int i;                              // Software timer

   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = CONFIG_PAGE;              // Set SFR page

   OSCICN = 0x80;                      // Set internal oscillator to run
                                       // at its slowest frequency

   CLKSEL = 0x00;                      // Select the internal osc. as
                                       // the SYSTEMCLOCK source

   // Initialize external crystal oscillator to use 22.1184 MHz crystal

   OSCXCN = 0x67;                      // Enable external crystal osc.
   for (i=0; i < 256; i++);            // Wait at least 1ms

   while (!(OSCXCN & 0x80));           // Wait for crystal osc to settle

   CLKSEL = 0x01;                     
   // Select external crystal as SYSTEMCLOCK source

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFRPAGE
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and GPIO ports.
//
// Pinout:
//
// P0.0   digital   push-pull     UART TX
// P0.1   digital   open-drain    UART RX
// P1.6   digital   push-pull     LED
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = CONFIG_PAGE;              // Set SFR page

   XBR0     = 0x04;                    // Enable UART0
   XBR1     = 0x00;
   XBR2     = 0x40;                    // Enable crossbar and weak pull-up


   P0MDOUT |= 0x01;                    // Set TX pin to push-pull
   P1MDOUT |= 0x40;                    // Set P1.6(LED) to push-pull

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// UART0_Init   Variable baud rate, Timer 2, 8-N-1
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure UART0 for operation at <BAUDRATE> 8-N-1 using Timer2 as
// baud rate source.
//
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
   char SFRPAGE_SAVE;

   SFRPAGE_SAVE = SFRPAGE;             // Preserve SFRPAGE

   SFRPAGE = TMR2_PAGE;

   TMR2CN = 0x00;                      // Timer in 16-bit auto-reload up timer
                                       // mode
   TMR2CF = 0x08;                      // SYSCLK is time base; no output;
                                       // up count only
   RCAP2 = - ((long) SYSTEMCLOCK/BAUDRATE/16);
   TMR2 = RCAP2;
   TR2= 1;                             // Start Timer2

   SFRPAGE = UART0_PAGE;

   SCON0 = 0x50;                       // 8-bit variable baud rate;
                                       // 9th bit ignored; RX enabled
                                       // clear all flags
   SSTA0 = 0x15;                       // Clear all flags; enable baud rate
                                       // doubler (not relevant for these
                                       // timers);
                                       // Use Timer2 as RX and TX baud rate
                                       // source;
   ES0 = 1;
   IP |= 0x10;
   SFRPAGE = SFRPAGE_SAVE;             // Restore SFRPAGE
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// UART0_Interrupt
//-----------------------------------------------------------------------------
//
// This routine is invoked whenever a character is entered or displayed on the
// Hyperterminal.
//
//-----------------------------------------------------------------------------

void UART0_Interrupt (void) interrupt 4
{
   SFRPAGE = UART0_PAGE;

   if (RI0 == 1)
   {
      // Check if a new word is being entered
      if( UART_Buffer_Size == 0) {
         UART_Input_First = 0; }

      RI0 = 0;
      Byte = SBUF0;   // Read a character from Hyperterminal
     
      if (UART_Buffer_Size < UART_BUFFERSIZE)
      {
         UART_Buffer[UART_Input_First] = Byte;

         UART_Buffer_Size++;            // Update array's size

         UART_Input_First++;            // Update counter
      }
   }

   if (TI0 == 1)           // Check if transmit flag is set
   {
      TI0 = 0;

      if (UART_Buffer_Size != 1)        // If buffer not empty
      {
         // Check if a new word is being output
         if ( UART_Buffer_Size == UART_Input_First )  {
              UART_Output_First = 0;   }

         Byte = UART_Buffer[UART_Output_First];

         if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If upper case letter
            Byte -= 32; }

         SBUF0 = Byte;                   // Transmit to Hyperterminal

         UART_Output_First++;             // Update counter

         UART_Buffer_Size--;              // Decrease array size
      }
      else
      {
         UART_Buffer_Size = 0;          // Set the array size to 0
         TX_Ready = 1;                  // Transmission complete
      }
   }
}
                                                                          
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

使用特权

评论回复
11
xlsbz| | 2011-4-8 17:07 | 只看该作者
交叉开关

使用特权

评论回复
12
wpw75101| | 2013-5-18 23:01 | 只看该作者
请问你现在解决了吗?如何解决的?我也遇到这个问题了!

使用特权

评论回复
13
laoxu| | 2013-5-19 07:00 | 只看该作者
肯定是交叉开关未设置正确!!!

初上手者,基本上都会犯的低级错误~~~ :lol

使用特权

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

本版积分规则

个人签名:烟台电工@張紹言

32

主题

1934

帖子

8

粉丝