📄 f12x_math_mac.c
字号:
//-----------------------------------------------------------------------------
// F12x_Math_MAC.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program uses the built-in Keil MAC routines to perform basic math,
// using the UART to interface with the user.
//
// How To Test:
//
// 1) Download code to a 'F12x device that is connected to a UART transceiver
// 2) Connect serial cable from the transceiver to a PC
// 3) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the COM port at <BAUDRATE> and 8-N-1
// 4) Run the code:
// - the firmware will print a menu and prompt for a desired Math function
// - the ASM code can be viewed by using the "#pragma SRC" directive
//
// Target: C8051F12x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
// Release 1.0
// -Initial Revision (TP)
// -19 JUN 2006
//
//-----------------------------------------------------------------------------
// Uncomment this to generate the assembly version to view the MAC code
// #pragma SRC (F12x_Math_MAC.asm)
// Use the built-in Keil C8051F12x MAC routines
// The evaluation version of the Keil compiler does not support these built-in
// routines.
#pragma mdu_f120
// signed int mul = ?C?IMUL_F120 routine
// unsigned int mul = ?C?IMUL_F120 routine
// signed long mul = ?C?LMUL_F120 routine
// unsigned long mul = ?C?LMUL_F120 routine
// signed long shift left = ?C?LSHL_F120 routine
// unsigned long shift left = ?C?LSHL_F120 routine
// signed long shift right = ?C?ULSHR_F120 routine
// unsigned long shift right = ?C?SLSHR_F120 routine
// Note: The arithmetic unit does not allow reentrant code and cannot be used
// in multiple threads or the main and interrupt routines at the same time.
// You may therefore need to disable the MDU unit with NOMOD_F120.
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F120.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F12x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 RCAP3 = 0xca; // Timer3 capture/reload
sfr16 RCAP4 = 0xca; // Timer4 capture/reload
sfr16 TMR2 = 0xcc; // Timer2
sfr16 TMR3 = 0xcc; // Timer3
sfr16 TMR4 = 0xcc; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd2; // DAC1 data
sfr16 PCA0CP5 = 0xe1; // PCA0 Module 5 capture
sfr16 PCA0CP2 = 0xe9; // PCA0 Module 2 capture
sfr16 PCA0CP3 = 0xeb; // PCA0 Module 3 capture
sfr16 PCA0CP4 = 0xed; // PCA0 Module 4 capture
sfr16 PCA0 = 0xf9; // PCA0 counter
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 capture
sfr16 PCA0CP1 = 0xfd; // PCA0 Module 1 capture
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define BAUDRATE 57600 // Baud rate of UART in bps
#define IntOsc 24500000 // Internal oscillator frequency in Hz
#define SYSCLK 49000000 // Output of PLL derived from (IntOsc*2)
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void UART1_Init (void);
void Timer3_Init (int counts);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
char key_press;
// 16-bit Unsigned Multiply
unsigned int u_multiplier, u_multiplicand, u_product;
// 16-bit Signed Multiply
int multiplier, multiplicand, product;
// 32-bit Unsigned Multiply
unsigned long ul_multiplier, ul_multiplicand, ul_product;
// 32-bit Signed Multiply
long l_multiplier, l_multiplicand, l_product;
// Random Generator Operations
unsigned long ulrand1, ulrand2;
long lrand1, lrand2;
unsigned char shifts;
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init (); // Initialize oscillator to 49 MHz
// using the PLL
UART1_Init (); // Initialize UART1
PORT_Init (); // Initialize crossbar and GPIO
while (1) // Serial port command decoder
{
SFRPAGE = UART1_PAGE;
// Output the menu
printf ("\n\nMAC Menu:\n\n");
printf ("1. 16-bit Unsigned Multiply\n");
printf ("2. 32-bit Unsigned Multiply\n");
printf ("3. 16-bit Signed Multiply\n");
printf ("4. 32-bit Signed Multiply\n");
printf ("5. 32-bit Unsigned Multiply Random Generator\n");
printf ("6. 32-bit Signed Multiply Random Generator\n");
printf ("7. 32-bit Left Shift Random Generator\n");
printf ("8. 32-bit Unsigned Right Shift Random Generator\n");
printf ("9. 32-bit Signed Right Shift Random Generator\n\n");
printf ("Enter a number:");
// Wait for a user selection
scanf ("%c", &key_press);
getchar();
// Decode the user selection
switch (key_press)
{
// 16-bit Unsigned Multiply
case '1':
{
printf ("\nEnter Multiplicand:");
scanf ("%u",&u_multiplicand);
getchar();
printf ("\nEnter Multiplier:");
scanf ("%u",&u_multiplier);
getchar();
u_product = u_multiplicand * u_multiplier;
printf ("\n16-bit Unsigned Multiply Product = ");
printf ("%u\n\n", u_product);
}
break;
// 32-bit Unsigned Multiply
case '2':
{
printf ("\nEnter Multiplicand:");
scanf ("%lu",&ul_multiplicand);
getchar();
printf ("\nEnter Multiplier:");
scanf ("%lu",&ul_multiplier);
getchar();
ul_product = ul_multiplicand * ul_multiplier;
printf ("\n32-bit Unsigned Multiply = ");
printf ("%lu\n\n", ul_product);
}
break;
// 16-bit Signed Multiply
case '3':
{
printf ("\nEnter Multiplicand:");
scanf ("%u",&multiplicand);
getchar();
printf ("\nEnter Multiplier:");
scanf ("%u",&multiplier);
getchar();
product = multiplicand * multiplier;
printf ("\n16-bit Signed Multiply: = ");
printf ("%d\n", product);
}
break;
// 32-bit Signed Multiply
case '4':
{
printf ("\nEnter Multiplicand:");
scanf ("%ld",&l_multiplicand);
getchar();
printf ("\nEnter Multiplier:");
scanf ("%ld",&l_multiplier);
getchar();
l_product = l_multiplicand * l_multiplier;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -