I'm student and found this code on Internet. Can anyone explain algorythm used here?
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int count=50;
clrscr();
while(count--)
{
sound(10*random(100));
delay(75);
nosound();
textattr(random(16)+'a'+BLINK);
cprintf("*");
}
}
Looks like Turbo C code. sound will set the "PC beeper" playing given frequency, so the code will produce 50 random tones in the loop. It will also use textattr to select random color (most common text modes had 16 fixed colors, random(16) is for that reason) with blink attribute set and print an asterisk 50 times along with the sounds. This will require a real DOS text mode which actually supports blinking characters, in modern console window there will be no blinking.
Also, the include files and libraries are not standard C libraries, so basically code requires Turbo C to work without modification. The code is from simpler era of PC software, where applications had entire computer for themselves, and often used text mode.
Related
Just bought an MSP430 launchpad
Been reading through stuff, but I'm unable to learn from all those verbose sources how to stick my C program inside the micro controller. They're not objective (I've started with the manuals that came within, then following hundreds of links on texas website. They are poorly informative).
TO BEAR IN MIND:
I'm a student;
My professor isn't of much help;
I'm completely new to this hardware stuff. Kind of new on C programming too... we can say a year of practice;
I consider the KISS principle a good practice: My teacher accomplishes a firework of LEDs with a .c file, a makefile, and a make.exe that I don't have the least idea of what is and how works.
Below, my steps taken so far: (They did NOT work. That's the reason I'm asking here. I would appreciate a very objective procedure/corrections at first, and later, the brainstorm)
Downloaded mspgcc-20120406-p20120911
Installed that on Code::Blocks, using Settings > Compiler - Toolchain Executables tab
(I've tried Energia, but doesn't seem like a very orthodox .c editor. And I love codeblocks, or devcpp, or, as a third option, notepad++, or even Eclipse)
I've also tried CodeComposerStudio. After downloading packages and starting a full project answering neverending questions, I still don't know how to flash the code.
I even made a simple program to blink a led:
#include <stdio.h>
#include <msp430f5529.h>
#include <msp430.h>
void ConfigureCpu(void)
{
WDTCTL = 0x5a80;
// ACLK = 32768 Hz
// MCLK = 16000000 Hz
// SMCLK = 16000000 Hz
DCOCTL = 0x74;
BCSCTL1 = 0x0F;
BCSCTL2 = 0x88;
BCSCTL3 = 0x84;
P4DIR = 0xff;
}
void delayms(t)
{
int i, x;
for(i=0;i<=t;i++)
for(x=0;x<16000;x++);
}
int main()
{
ConfigureCpu();
while(1)
{
P4OUT=0x42; //0100 0010 = 0x42
delayms(1000);
P4OUT=0x00;
delayms(1000);
}
}
It doesn't work for two reasons:
'DCOCTL' undeclared (first use in this function)|
And if I comment those registers, I get the following:
cannot open linker script file memory.x||No such file or directory|
.
L:\MSP\ is my directory for everything related to this journey
Example:
L:\MSP\GCC
L:\MSP\Flasher\
L:\MSP\Programs //my .c are stored here
(Code::Blocks is installed on the root of C:)
I would like someone to tell me what I need to learn (step by step, if possible, from the very beginning, to the very end - from choosing tools, to uploading to the controller)
The flyer in the box describes what to do.
Go to www.ti.com/launchpad and download either Code Composer Studio 4 or the IAR Embedded Workbench trial.
The TI page also holds all other information that you might need.
Looking at msp430f5529.h file on my system, it seems like this chip doesn't have those registers or they aren't in the header. The code just sets the register to a magic number with no explanation, comment, or calculation, so it's hard to figure out what the purpose is. (It's probably trying to configure clocks.)
Second, you'll probably need that linker file for your chip so the compiler (linker stage) can set up correct memory addresses.
The code actually looks like a blinky, but meant for a different chip or compiler. It would probably work but you'd need to change the parts that are different (port it) for your setup. It would be educational, but perhaps not what you want to spend your time on.
To solve both problems, I'd start by getting a project meant for the particular chip/board you have. Build that and see if it works. Then you know your compiler and the project work. You can go on to modify it for your application.
I am trying to make a program which includes a beep noise. I work on a 32 bit Windows Vista. I am using the Code::Blocks IDE which runs on a GNU compiler. My sample code is -
#include <stdio.h>
#include <windows.h>
#include <dos.h>
int main(void)
{
Beep(750, 300);
printf("\n \n \t This is a dummy program for Beep.");
getch();
return 0;
}
On the Internet I read that we could also use \a in printf to make a beep. I tried that but it is not working. I checked my speakers and sound card. Everything is perfect but I hear no beep. Even the method I displayed in my sample code does not work.
The C standard recommends that writing '\a' to standard output produce an audible or visible alert signal, but it will not work if standard output is redirected. Likewise, some newer computers lack the PC beeper on which Windows Beep() and some terminals rely. To cause a Windows PC to play an alert sound in a desktop application, you can call the Windows-specific MessageBeep function, which plays a sound "asynchronously" (in the background while your program continues to run). The user can configure which sound is associated with each of these four values in the Sound control panel.
#include <windows.h>
/* Include one of these in a function */
MessageBeep(MB_OK); /* play Windows default beep */
MessageBeep(MB_ICONINFORMATION); /* play asterisk sound */
MessageBeep(MB_ICONQUESTION); /* play question sound */
MessageBeep(MB_ICONWARNING); /* play warning sound */
MessageBeep() is defined in User32.dll, so if this gives you link errors, make sure you're linking to the corresponding import library. In MinGW GCC (the compiler in Code::Blocks), add -lUser32 to the list of libraries passed to the linker.
MessageBeep(-1);
From the MSDN documentation:
MessageBeep function
Plays a waveform sound. The waveform sound for each sound type is
identified by an entry in the registry.
BOOL WINAPI MessageBeep( _In_ UINT uType );
... ...
Value for uType: 0xFFFFFFFF
Meaning: A simple beep. If the
sound card is not available, the sound is generated using the speaker.
Also, and to my surprise, I've tested that. at least Windows 7 32 bits (and Windows Vista surely too) do some sort of emulation for the old 8253 I/O ports and the keyboard port, available to ring 3 processes, so the old implementation of sound() and nosound() should work. Unfornately, I haven't got any 32 bit machine available ATM so I cannot confirm this.
Beep does work again in Windows since windows 7. The format is:
Beep(frequency, duration) where frequency is the pitch in hertz, and duration is the length in milliseconds
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx
For the Beep() function in windows.h to actually work, you have to have a "PC speaker" buzzer in your PC, as stated in the function's documentation. So you need to have a fairly old PC and with Windows XP or older, since support for the function was apparently dropped in Windows Vista.
On newer Windows versions, calling Beep() gives a beep in the speakers instead, using your sound card. If you aren't getting any beep, it is possibly not related to the program, but perhaps to your specific computer hardware.
This one works to on Windows 7 compiled with Visual Studio 2017. No problems with that.
printf("\n Bad request - check status code parameter\a");
You can use \a. At least it works in my computer.
I'm trying out some old code frome the book "Black art of 3d game programming". I know it is outdated but I started reading it and it's kind of fun and interesting. I downloaded the OpenWatcom C Compiler and made a new DOS Project in order to get this old code even compiled. I already compiled on piece of code where Videomode int13h is set and then I was able to draw pixels to the screen. But this was done with a C function called _setvideomode(). In the following example the videomode is set via the _int86 function which makes the interrupt call and the prototype should be in bios.h, but OpenWatcom says: No prototype found for function _int86. I am stuck now and don't know what to do ;) Here is the code:
void setGraphxMode(int mode){
union REGS inregs,outregs;
inregs.h.ah = 0;
inregs.h.al = (unsigned char)mode;
_int86(0x10,&inregs,&outregs);
}
int main(){
return 0;
}
Would appreciate any advise on this and yes I know: Graphics are done via DirectX or OpenGL these days. This is just for learning purpose! Thank you :)
Under OpenWatcom the call you are looking for is int386 I believe:
int386(0x10, &inregs, &outregs);
I found this header file on google to perform mouse related events in C program but i am having no idea of int86 union REGS i,o; and what are other int86 type functions available in C? I am using windows OS and Turbo C.
#include<conio.h>
#include<stdio.h>
#include<dos.h>
initmouse();
void showmouseptr();
void restrictmousept(int,int,int,int);
void getmousepos(int *,int *,int *);
void hidemouseptr();
union REGS i,o;
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
void getmousepos(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
void hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}
This looks like old code for MS-DOS to user the x86 interrupts to call system functions to get mouse coordinates. I would be surprised if this code still works in a command prompt of any modern computer. In fact, the header file at the top including dos.h would give this away.
in86() is how you "interrupt" the CPU in DOS mode. It is sort of the way functions are called in modern day operating systems. More info here: http://wiki.answers.com/Q/What_is_the_INT86_function_in_C_programming
A union is a method in C of defining data which can be accessed in different ways. More here: http://www.go4expert.com/forums/showthread.php?t=15.
It is unclear what you are trying to do, let alone what operating system you are running under. You probably want to pick a language first (C# might be a good start, assuming you are using Windows) and then look at the base class library for WinForms to learn how to respond to mouse events. There are fine tutorials on WinForms which will teach you how to respond to the mouse.
Discard that code: it is old 16-bit DOS which probably is not what you want.
It seems you're rather new to C. In that case, a better advice is to study well the language; check around the site for good learning references. "The C Programming Language", by Dennis Ritchie and Brian Kernighan is a good starting point.
Having said that, there are no int86 functions "in C". These are Borland Turbo C extensions. That was how one could manipulate the mouse in DOS, but nowadays it is different.
Note that the C language is, fundamentally, simply a programming language. It is intended to express algorithms. However, the C language standard also gives you a standard library: a set of predefined types, functions and macros you can use, to save time and portability. This library addresses some basic functionality, and mouse input handling is not one of them.
In other words, after you have studied C, you'll be ready to deal with specific operating system's APIs. This is needed, because the C standard library offers no way to manipulate any sort of input devices. You will also study some fundamentals regarding how input is handled in modern operating systems. For example, to handle mouse input you will most likely need to create a window (or widget) in order to receive mouse events -- implying that you will have to read a bit about the underlying windowing API.
There are some libraries which accesses the system's APIs for you and factor out the differences, offering you a programming alternative. It should be no different, though, from studying and using the system's API directly, except possibly for enhanced portability.
The int86 function calls the DOS Mouse Interrupt 33h. A dos interrupt is specific to the msdos operating system, so your code is only usable under msdos. 'union REGS' are the input and output registers that are used as parameters for the DOS interrupt. A DOS interrupt is similar to a system call in other operating systems.
You could use dosbox if you want to run turbo C programs under Windows/Linux etc
For example, on Windows, just make a desktop link that runs a batch script to run your program in dosbox, this is how the steam version of Doom works I think
Let say I have 5 small piece of codes in C.
Every time I want to test each piece of code, I have to repeat this process:
#include <stdio.h>
int main()
{
// code piece go into here
return 0;
}
Is there way that I don't have to do this 5 times? I'm using Code::Blocks, that means I have to create 5 different projects, which I believe not necessary because each piece of code is small.
Is this really so hard? Every program you run needs a main function, and the text you've pasted there isn't very long. Also, people expect to see a main function in C/C++ programs. If you template this out somehow, you're just going to make your code confusing.
If the issue is that you have to make a project for every test you want to build, then I would guess you are not using your IDE correctly. Is there not a multi-target project type that lets you have multiple test programs without all the extra project files? If there is not, then perhaps you should be using a different IDE.
Use a good editor with code templates. Most feature-full editors (Emacs, vi, Scite, Textmate, or even MSVC if that's your cup of tea) have some support for them. This way, writing this boring template every time will take only a fraction of the second.
Would template files or copying and pasting be too difficult for some reason?