C发声程序
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <conio.h>
typedef struct {
short int pitch;
short int duration;
} NOTE;
NOTE notes[] = {{14, 500}, {16, 500}, {12, 500}, {0, 500}, {7, 1000}};
void setfreq(int hz)
{
hz = 1193180 / hz; // clocked at 1.19MHz
_outp(0x43, 0xb6); // timer 2, square wave
_outp(0x42, hz);
_outp(0x42, hz >> 8);
}
void playnote(NOTE note)
{
_outp(0x61, _inp(0x61) | 0x03); // start speaker going
setfreq((int)(400 * pow(2, note.pitch / 12.0)));
Sleep(note.duration);
_outp(0x61, _inp(0x61) & ~0x03); // stop that racket!
}
int main(int argc, char* argv[])
{
int i;
HANDLE h;
h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(h == INVALID_HANDLE_VALUE)
{
printf("Couldn't access giveio device\n");
return -1;
}
for(i=0; i < sizeof(notes)/sizeof(int); ++i)
playnote(notes[i]);
CloseHandle(h);
return 0;
}
有几点需要注意:
1,\\\\.\\giveio是打开的设备,由于是用\\转义表示\,所以上述其实是表示\\ . \
2,这种底层的编程很少。以上_outp访问的是底层寄存器,功能如注
相关文档:
曾经碰到过让你迷惑不解、类似于int * (* (*fp1) (int) ) [10];这样的变量声明吗?本文将由易到难,一步一步教会你如何理解这种复杂的C/C++声明。
我们将从每天都能碰到的较简单的声明入手,然后逐步加入const修饰符和typedef,还有函数指针,最后介绍一个能够让你准确地理解任何C/C++声明的&ld ......
extern "C" 详解
在C++中,为了支持重载机制,在编译生成汇编代码时,函数的名字要加入函数的参数类型或者返回值类型等信息
在C中,因没有重载机制,编译后的代码只是简单的函数名字而已,不加入其他的信息
1. 不加入extern "C"
testexternc.cpp
int mytest(void)
{
int a=10,b=20;
int c=a+b;
ret ......
Data Type Ranges
C/C++ recognizes the types shown in the table below.
Type Name Bytes Other Names Range of Values
&nb ......
在这里贴上最近自己忙活的用turbo C编写的“打字游戏”的源代码:
#include<graphics.h>
#include<conio.h>
#include<STDLIB.h>
#include<dos.h>
#define BK_COLOR BLACK
#define CHAR_COLOR WHITE
#define C_COLOR BLUE
#define num 10
#define SPEED 3000
#define Esc 27
# ......
#include <stdio.h>
#define N 19
int main()
{
int i;
for (i=0;i<=N;i++)
{
printf("%*.*s%-*.*s\n",N,i<=N/2?i:N-1,"*******************",\
N,i<=N/2?i+1:N-i+1,"*******************");
}
return 0;
}
%m.ns ......