📄 f12x_math_mac.c
字号:
printf ("\n32-bit Signed Multiply = ");
printf ("%ld\n\n", l_product);
}
break;
// 32-bit Unsigned Multiply Random Generator
case '5':
{
ulrand1 = 5543408L; // Start with some number
// Generate a "pseudo" random number
ulrand1 = 1664525L * ulrand1 + 1013904223L;
ulrand2 = 1664525L * ulrand2 + 1013904223L;
ul_product = ulrand1 * ulrand2;
printf ("\n32-bit Unsigned Random Number = ");
printf ("%lu\n\n", ul_product);
}
break;
// 32-bit Signed Multiply Random Generator
case '6':
{
lrand1 = 5543408L; // Start with some number
// Generate a "pseudo" random number
lrand1 = 1664525L * lrand1 + 1013904223L;
lrand2 = 1664525L * lrand2 + 1013904223L;
l_product = lrand1 * lrand2;
printf ("\n32-bit Signed Random Number = ");
printf ("%l\n\n", ul_product);
}
break;
// 32-bit Left Shift Random Generator
case '7':
{
ulrand1 = 5543408L; // Start with some number
// Generate a "pseudo" random number
for(shifts = 0; shifts < 32; shifts++)
{
ulrand1 = 1664525L * ulrand1 + 1013904223L;
ul_product = ulrand1 << shifts;
printf ("\n32-bit Left Shift Random Number: ");
printf ("%lx shifted left by %bu",ulrand1, shifts);
}
}
break;
// 32-bit Unsigned Right Shift Random Generator
case '8':
{
ulrand1 = 5543408L; // Start with some number
// Generate a "pseudo" random number
for(shifts = 0; shifts < 32; shifts++)
{
ulrand1 = 1664525L * ulrand1 + 1013904223L;
ul_product = ulrand1 >> shifts;
printf ("\n32-bit Unsigned Right Shift Random Number: ");
printf ("%lx shifted right by %bu",ulrand1, shifts);
}
}
break;
// 32-bit Signed Right Shift Random Generator
case '9':
{
lrand1 = 5543408L; // Start with some number
// Generate a "pseudo" random number
for(shifts = 0; shifts < 32; shifts++)
{
lrand1 = 1664525L * lrand1 + 1013904223L;
l_product = lrand1 >> shifts;
printf ("\n32-bit Signed Right Shift Random Number: ");
printf ("%lx shifted right by %bu",lrand1, shifts);
}
}
break;
default: // Indicate unknown command;
printf ("\nUnknown command: '%c'\n", key_press);
break;
} // Menu Decode switch()
} // loop forever
while(1);
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes the system clock to use the internal oscillator
// at 24.5 MHz multiplied by two using the PLL.
//
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
int i; // software timer
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCICN = 0x83; // set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSCLK source
//Turn on the PLL and increase the system clock by a factor of M/N = 2
SFRPAGE = CONFIG_PAGE;
PLL0CN = 0x00; // Set internal osc. as PLL source
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10; // Set FLASH read time for 50MHz clk
// or less
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01; // Enable Power to PLL
PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1)
PLL0FLT = 0x01; // Set the PLL filter register for
// a reference clock from 19 - 30 MHz
// and an output clock from 45 - 80 MHz
PLL0MUL = 0x02; // Multiply SYSCLK by M (M = 2)
for (i=0; i < 256; i++) ; // Wait at least 5us
PLL0CN |= 0x02; // Enable the PLL
while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked
CLKSEL = 0x02; // Select PLL as SYSCLK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// UART1_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the UART1 using Timer1, for <BAUDRATE> and 8-N-1.
//
//-----------------------------------------------------------------------------
void UART1_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
#if (SYSCLK/BAUDRATE/2/256 < 1)
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
#elif (SYSCLK/BAUDRATE/2/256 < 4)
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
#elif (SYSCLK/BAUDRATE/2/256 < 12)
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
#elif (SYSCLK/BAUDRATE/2/256 < 48)
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
#else
#error "Unable to generate specified baudrate!"
#endif
TL1 = TH1; // initialize Timer1
TR1 = 1; // start Timer1
SFRPAGE = UART1_PAGE;
TI1 = 1; // Indicate TX1 ready
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0 digital push-pull UART1 TX
// P0.1 digital open-drain UART1 RX
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-up
// Enable UART1
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -